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
14 changes: 14 additions & 0 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,20 @@ 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;
}
if (getimagesize($imagePath) !== false) {
$this->resource = @imagecreatefromavif($imagePath);
Fixed Show fixed Hide fixed
} else {
$this->logger->debug('OC_Image->loadFromFile, AVIF image not valid: ' . $imagePath, ['app' => 'core']);
}
} 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