Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AVIF Thumbnail Generation #40048

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@
'OC\\Preview\\Watcher' => $baseDir . '/lib/private/Preview/Watcher.php',
'OC\\Preview\\WatcherConnector' => $baseDir . '/lib/private/Preview/WatcherConnector.php',
'OC\\Preview\\WebP' => $baseDir . '/lib/private/Preview/WebP.php',
'OC\\Preview\\AVIF' => $baseDir . '/lib/private/Preview/AVIF.php',
'OC\\Preview\\XBitmap' => $baseDir . '/lib/private/Preview/XBitmap.php',
'OC\\Profile\\Actions\\EmailAction' => $baseDir . '/lib/private/Profile/Actions/EmailAction.php',
'OC\\Profile\\Actions\\FediverseAction' => $baseDir . '/lib/private/Profile/Actions/FediverseAction.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Preview\\Watcher' => __DIR__ . '/../../..' . '/lib/private/Preview/Watcher.php',
'OC\\Preview\\WatcherConnector' => __DIR__ . '/../../..' . '/lib/private/Preview/WatcherConnector.php',
'OC\\Preview\\WebP' => __DIR__ . '/../../..' . '/lib/private/Preview/WebP.php',
'OC\\Preview\\AVIF' => __DIR__ . '/../../..' . '/lib/private/Preview/AVIF.php',
'OC\\Preview\\XBitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/XBitmap.php',
'OC\\Profile\\Actions\\EmailAction' => __DIR__ . '/../../..' . '/lib/private/Profile/Actions/EmailAction.php',
'OC\\Profile\\Actions\\FediverseAction' => __DIR__ . '/../../..' . '/lib/private/Profile/Actions/FediverseAction.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@

/** @deprecated 29.0.0 Use OCP\Collaboration\Reference\LinkReferenceProvider instead */
class LinkReferenceProvider extends OCPLinkReferenceProvider {
}
}
20 changes: 20 additions & 0 deletions lib/private/Preview/AVIF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace OC\Preview;

use OCP\Files\FileInfo;

class AVIF extends Image {
/**
* {@inheritDoc}
*/
public function getMimeType(): string {
return '/image\/avif/';
}

public function isAvailable(FileInfo $file): bool {
return (bool)(imagetypes() & IMG_AVIF);

Check failure

Code scanning / Psalm

UndefinedConstant Error

Const IMG_AVIF is not defined
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add IMG_AVIF using define for older PHP versions below 8.1 or check for support. However, it's recommended to use a higher PHP version for Nextcloud, so I don't think it's a real issue.

}
}
2 changes: 2 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ private function getExtension($mimeType) {
return 'png';
case 'image/jpeg':
return 'jpg';
case 'image/avif':
return 'avif';
case 'image/webp':
return 'webp';
case 'image/gif':
Expand Down
2 changes: 2 additions & 0 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ protected function getEnabledDefaultProvider() {
Preview\XBitmap::class,
Preview\Krita::class,
Preview\WebP::class,
Preview\AVIF::class,
];

$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
Expand Down Expand Up @@ -361,6 +362,7 @@ protected function registerCoreProviders() {
$this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/');
$this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
$this->registerCoreProvider(Preview\WebP::class, '/image\/webp/');
$this->registerCoreProvider(Preview\AVIF::class, '/image\/avif/');
$this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/');
$this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
Expand Down
101 changes: 100 additions & 1 deletion lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class OC_Image implements \OCP\IImage {

// Default quality for jpeg images
protected const DEFAULT_JPEG_QUALITY = 80;
protected const DEFAULT_WEBP_QUALITY = 80;
protected const DEFAULT_AVIF_QUALITY = 50;

// Default quality for webp images
protected const DEFAULT_WEBP_QUALITY = 80;
Expand Down Expand Up @@ -271,6 +273,12 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case 'image/jpeg':
$imageType = IMAGETYPE_JPEG;
break;
case 'image/webp':
$imageType = IMAGETYPE_WEBP;
break;
case 'image/avif':
$imageType = IMAGETYPE_JPEG;
break;
case 'image/png':
$imageType = IMAGETYPE_PNG;
break;
Expand All @@ -289,6 +297,19 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
}
}

if ($this->mimeType !== 'image/gif') {
$preview_format = $this->config->getSystemValueString('preview_format', '');
switch ($preview_format) {
case 'webp':
$imageType = IMAGETYPE_WEBP;
break;
case 'avif':
$imageType = IMAGETYPE_AVIF;

Check failure

Code scanning / Psalm

UndefinedConstant Error

Const IMAGETYPE_AVIF is not defined
break;
default:
}
}

switch ($imageType) {
case IMAGETYPE_GIF:
$retVal = imagegif($this->resource, $filePath);
Expand All @@ -298,6 +319,16 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality());
break;
case "image/webp":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$retVal = imagewebp($this->resource, $filePath, $this->getWebpQuality());
break;
case "image/avif":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80100 ? true : 1));
$retVal = imageavif($this->resource, $filePath, $this->getAvifQuality(), 10);

Check failure

Code scanning / Psalm

UndefinedFunction Error

Function imageavif does not exist
break;
case IMAGETYPE_PNG:
$retVal = imagepng($this->resource, $filePath);
break;
Expand Down Expand Up @@ -353,9 +384,23 @@ public function dataMimeType(): ?string {
return null;
}

if ($this->mimeType !== 'image/gif') {
$preview_format = $this->config->getSystemValueString('preview_format', '');
switch ($preview_format) {
case 'webp':
return 'image/webp';
case 'avif':
return 'image/avif';
default:
}
}

switch ($this->mimeType) {
case 'image/png':
case 'image/jpeg':
case 'image/webp':

Check failure

Code scanning / Psalm

TypeDoesNotContainType Error

Type never for $this->mimeType is never =string(image/webp)
case 'image/avif':
return 'image/jpeg';
case 'image/gif':
case 'image/webp':
return $this->mimeType;
Expand All @@ -372,7 +417,23 @@ public function data(): ?string {
return null;
}
ob_start();
switch ($this->mimeType) {
$imageType = $this->imageType;
if ($imageType == 'image/avif') {
$imageType = 'image/jpeg';
}
if ($imageType !== 'image/gif') {
$preview_format = $this->config->getSystemValueString('preview_format', '');
switch ($preview_format) {
case 'webp':
$imageType = 'image/webp';
break;
case 'avif':
$imageType = 'image/avif';
break;
default:
}
}
switch ($imageType) {
case "image/png":
$res = imagepng($this->resource);
break;
Expand All @@ -381,6 +442,16 @@ public function data(): ?string {
$quality = $this->getJpegQuality();
$res = imagejpeg($this->resource, null, $quality);
break;
case "image/webp":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$res = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case "image/avif":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80100 ? true : 1));
$res = imageavif($this->resource, null, $this->getAvifQuality(), 10);

Check failure

Code scanning / Psalm

UndefinedFunction Error

Function imageavif does not exist
break;
case "image/gif":
$res = imagegif($this->resource);
break;
Expand Down Expand Up @@ -417,6 +488,24 @@ protected function getJpegQuality(): int {
return min(100, max(10, (int) $quality));
}

protected function getWebpQuality(): int {
$quality = $this->config->getAppValue('preview', 'webp_quality', (string) self::DEFAULT_WEBP_QUALITY);
// TODO: remove when getAppValue is type safe
if ($quality === null) {
$quality = self::DEFAULT_WEBP_QUALITY;
}
return min(100, max(10, (int) $quality));
}

protected function getAvifQuality(): int {
$quality = $this->config->getAppValue('preview', 'avif_quality', (string) self::DEFAULT_AVIF_QUALITY);
// TODO: remove when getAppValue is type safe
if ($quality === null) {
$quality = self::DEFAULT_AVIF_QUALITY;
}
return min(100, max(10, (int) $quality));
}

/**
* @return int
*/
Expand Down Expand Up @@ -722,6 +811,16 @@ public function loadFromFile($imagePath = false) {
$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_AVIF:

Check failure

Code scanning / Psalm

UndefinedConstant Error

Const IMAGETYPE_AVIF is not defined
if (imagetypes() & IMG_AVIF) {
Fixed Show fixed Hide fixed

Check failure

Code scanning / Psalm

UndefinedConstant Error

Const IMG_AVIF is not defined
if (!$this->checkImageSize($imagePath)) {
return false;
}
$this->resource = @imagecreatefromavif($imagePath);

Check failure

Code scanning / Psalm

UndefinedFunction Error

Function imagecreatefromavif does not exist
} else {
$this->logger->debug('OC_Image->loadFromFile, AVIF images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_BMP:
$this->resource = imagecreatefrombmp($imagePath);
break;
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Collaboration/Reference/LinkReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class LinkReferenceProvider implements IReferenceProvider {
'image/jpeg',
'image/gif',
'image/svg+xml',
'image/webp'
'image/webp',
'image/avif'
];

/**
Expand Down
1 change: 1 addition & 0 deletions resources/config/mimetypemapping.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"webloc": ["application/internet-shortcut"],
"webm": ["video/webm"],
"webp": ["image/webp"],
"avif": ["image/avif"],
"wmv": ["video/x-ms-wmv"],
"woff": ["application/font-woff"],
"wpd": ["application/vnd.wordperfect"],
Expand Down