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 @@ -1506,6 +1506,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 @@ -1539,6 +1539,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 @@ -51,7 +51,8 @@ class LinkReferenceProvider implements IReferenceProvider {
'image/jpeg',
'image/gif',
'image/svg+xml',
'image/webp'
'image/webp',
'image/avif'
];

private IClientService $clientService;
Expand Down
41 changes: 41 additions & 0 deletions lib/private/Preview/AVIF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
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/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
73 changes: 73 additions & 0 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 = 80;

/** @var false|resource|\GdImage */
protected $resource = false; // tmp resource.
Expand Down Expand Up @@ -273,6 +275,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_AVIF;
Fixed Show fixed Hide fixed
break;
case 'image/png':
$imageType = IMAGETYPE_PNG;
break;
Expand All @@ -297,6 +305,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 >= 80100 ? 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

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

if ($this->mimeType !== 'image/gif') {
$preview_format = $this->config->getSystemValueString('preview_format', 'jpeg');

switch ($preview_format) { // Change the format to the correct one
case 'webp':
return 'image/webp';
break;
case 'avif':
return 'image/avif';
break;
default:
}
}

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

Check failure

Code scanning / Psalm

TypeDoesNotContainType

Type never for $this->mimeType is never =string(image/webp)
case 'image/avif':
return 'image/jpeg';
case 'image/gif':
return $this->mimeType;
default:
Expand All @@ -388,6 +423,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 >= 80100 ? 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

Function imageavif does not exist
break;
case "image/gif":
$res = imagegif($this->resource);
break;
Expand Down Expand Up @@ -421,6 +466,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));
}

/**
* (I'm open for suggestions on better method name ;)
* Get the orientation based on EXIF data.
Expand Down Expand Up @@ -714,6 +777,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

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

Check failure

Code scanning / Psalm

UndefinedConstant

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

Check failure

Code scanning / Psalm

UndefinedFunction

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
1 change: 1 addition & 0 deletions resources/config/mimetypemapping.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,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