Skip to content

Commit

Permalink
feat: add AVIF support
Browse files Browse the repository at this point in the history
Signed-off-by: Anderson Entwistle <[email protected]>
  • Loading branch information
aentwist committed Apr 4, 2023
1 parent d38f33f commit 8d09ec9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@
'OC\\OCS\\Result' => $baseDir . '/lib/private/OCS/Result.php',
'OC\\PreviewManager' => $baseDir . '/lib/private/PreviewManager.php',
'OC\\PreviewNotAvailableException' => $baseDir . '/lib/private/PreviewNotAvailableException.php',
'OC\\Preview\\Avif' => $baseDir . '/lib/private/Preview/Avif.php',
'OC\\Preview\\BMP' => $baseDir . '/lib/private/Preview/BMP.php',
'OC\\Preview\\BackgroundCleanupJob' => $baseDir . '/lib/private/Preview/BackgroundCleanupJob.php',
'OC\\Preview\\Bitmap' => $baseDir . '/lib/private/Preview/Bitmap.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 @@ -1427,6 +1427,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\OCS\\Result' => __DIR__ . '/../../..' . '/lib/private/OCS/Result.php',
'OC\\PreviewManager' => __DIR__ . '/../../..' . '/lib/private/PreviewManager.php',
'OC\\PreviewNotAvailableException' => __DIR__ . '/../../..' . '/lib/private/PreviewNotAvailableException.php',
'OC\\Preview\\Avif' => __DIR__ . '/../../..' . '/lib/private/Preview/Avif.php',
'OC\\Preview\\BMP' => __DIR__ . '/../../..' . '/lib/private/Preview/BMP.php',
'OC\\Preview\\BackgroundCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Preview/BackgroundCleanupJob.php',
'OC\\Preview\\Bitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/Bitmap.php',
Expand Down
19 changes: 19 additions & 0 deletions lib/private/Preview/Avif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace OC\Preview;

use OCP\Files\FileInfo;

class Avif extends Image {

public function getMimeType(): string {
return '/image\/avif/';
}

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

}
2 changes: 2 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ private function getExtention($mimeType) {
return 'gif';
case 'image/webp':
return 'webp';
case 'image/avif':
return 'avif';
default:
throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"');
}
Expand Down
2 changes: 2 additions & 0 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,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 @@ -368,6 +369,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
12 changes: 12 additions & 0 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ public function data(): ?string {
case "image/webp":
$res = imagewebp($this->resource, null, $quality);
break;
case "image/avif":
$res = imageavif($this->resource, null, $quality);
break;
default:
$res = imagepng($this->resource);
$this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
Expand Down Expand Up @@ -762,6 +765,15 @@ public function loadFromFile($imagePath = false) {
$this->logger->debug('OC_Image->loadFromFile, webp 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: installation does not support AVIF; got $imagePath", ['app' => 'core']);
}
break;
/*
case IMAGETYPE_TIFF_II: // (intel byte order)
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 @@ -16,6 +16,7 @@
"arw": ["image/x-dcraw"],
"asciidoc": ["text/asciidoc", "text/plain"],
"avi": ["video/x-msvideo"],
"avif": ["image/avif"],
"bash": ["text/x-shellscript"],
"bat": ["application/x-msdos-program"],
"bin": ["application/x-bin"],
Expand Down

0 comments on commit 8d09ec9

Please sign in to comment.