Skip to content

Add AVIF Thumbnail Generation #40048

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,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 @@ -1893,6 +1893,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 @@ -12,4 +12,4 @@

/** @deprecated 29.0.0 Use OCP\Collaboration\Reference\LinkReferenceProvider instead */
class LinkReferenceProvider extends OCPLinkReferenceProvider {
}
}
69 changes: 69 additions & 0 deletions lib/private/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Image implements IImage {
// Default quality for webp images
protected const DEFAULT_WEBP_QUALITY = 80;

// Default quality for avif images
protected const DEFAULT_AVIF_QUALITY = 50;

// tmp resource.
protected GdImage|false $resource = false;
// Default to png if file type isn't evident.
Expand Down Expand Up @@ -240,11 +243,27 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case 'image/webp':
$imageType = IMAGETYPE_WEBP;
break;
case 'image/avif':
$imageType = IMAGETYPE_JPEG;
break;
default:
throw new \Exception('Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
}
}

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;
break;
default:
}
}

switch ($imageType) {
case IMAGETYPE_GIF:
$retVal = imagegif($this->resource, $filePath);
Expand Down Expand Up @@ -273,6 +292,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case IMAGETYPE_WEBP:
$retVal = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case IMAGETYPE_AVIF:
$retVal = imageavif($this->resource, $filePath, $this->getAvifQuality(), 10);
break;
default:
$retVal = imagepng($this->resource, $filePath);
}
Expand Down Expand Up @@ -308,12 +330,25 @@ 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/gif':
case 'image/webp':
return $this->mimeType;
case 'image/avif':
return 'image/jpeg';
default:
return 'image/png';
}
Expand All @@ -327,6 +362,22 @@ public function data(): ?string {
return null;
}
ob_start();
$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 ($this->mimeType) {
case 'image/png':
$res = imagepng($this->resource);
Expand All @@ -342,6 +393,9 @@ public function data(): ?string {
case 'image/webp':
$res = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case "image/avif":
$res = imageavif($this->resource, null, $this->getAvifQuality(), 10);
break;
default:
$res = imagepng($this->resource);
$this->logger->info('Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
Expand Down Expand Up @@ -370,6 +424,11 @@ protected function getJpegQuality(): int {
return min(100, max(10, $quality));
}

protected function getAvifQuality(): int {
$quality = $this->config->getAppValue('preview', 'avif_quality', self::DEFAULT_AVIF_QUALITY);
return min(100, max(10, (int) $quality));
}

protected function getWebpQuality(): int {
$quality = $this->appConfig->getValueInt('preview', 'webp_quality', self::DEFAULT_WEBP_QUALITY);
return min(100, max(10, $quality));
Expand Down Expand Up @@ -674,6 +733,16 @@ public function loadFromFile($imagePath = false) {
$this->logger->debug('Image->loadFromFile, WBMP images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_AVIF:
if (imagetypes() & IMG_AVIF) {
if (!$this->checkImageSize($imagePath)) {
return false;
}
$this->resource = @imagecreatefromavif($imagePath);
} 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
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

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 @@ -598,6 +598,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 @@ -295,6 +295,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 @@ -342,6 +343,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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class LinkReferenceProvider implements IReferenceProvider, IPublicReferenceProvi
'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 @@ -216,6 +216,7 @@
"webloc": ["application/internet-shortcut"],
"webm": ["video/webm"],
"webp": ["image/webp"],
"avif": ["image/avif"],
"whiteboard": ["application/vnd.excalidraw+json"],
"wmv": ["video/x-ms-wmv"],
"woff": ["application/font-woff"],
Expand Down