Files
skjnldsv 1d780448e0 feat(preview): read AVIF through ImageMagick where libgd cannot
libgd carries AVIF only where the distribution built it against libavif.
Where it did not, ImageMagick often still can, and the server already
depends on it for HEIC.

The two are the same container with different codecs, which is what the
libvips author points out in h2non/imaginary#337: HEIF holds h265 for
HEIC and AV1 for AVIF, and ImageMagick reads both through libheif. So
HEIC's provider becomes a Heif base parameterised by the format it asks
ImageMagick for and the one it checks the build carries, and HEIC and
AVIFImagick are what is left of it.

Like the other ImageMagick providers this one is off until an admin
enables it, so libgd stays the default path and nothing changes for an
installation that never had the extension.

Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
2026-09-19 08:59:19 +02:00

169 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2018 ownCloud GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Preview;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\Image;
use OCP\Server;
use Psr\Log\LoggerInterface;
/**
* Creates a JPG preview of a HEIF file using ImageMagick via the PECL
* extension.
*
* HEIF is the container; what is inside it depends on the codec. HEIC holds
* h265 and AVIF holds AV1, and ImageMagick reads both through libheif, so
* the two providers differ only in which format they claim and which one
* they ask for by name.
*
* @package OC\Preview
*/
abstract class Heif extends ProviderV2 {
/**
* The ImageMagick format this provider reads, lowercase, used to tell
* it what the file is rather than leaving it to guess.
*/
abstract protected function formatHint(): string;
/**
* The name to ask ImageMagick whether it was built with, which is the
* codec's rather than the container's: a build may carry one and not
* the other.
*/
abstract protected function queryFormat(): string;
/**
* {@inheritDoc}
*/
#[\Override]
public function isAvailable(FileInfo $file): bool {
return in_array($this->queryFormat(), \Imagick::queryFormats($this->queryFormat()), true);
}
/**
* {@inheritDoc}
*/
#[\Override]
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
if (!$this->isAvailable($file)) {
return null;
}
$tmpPath = $this->getLocalFile($file);
if ($tmpPath === false) {
Server::get(LoggerInterface::class)->error(
'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
['app' => 'core']
);
return null;
}
// Creates \Imagick object from the file
try {
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
$bp->setFormat('jpg');
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->error(
'File: ' . $file->getPath() . ' Imagick says:',
[
'exception' => $e,
'app' => 'core',
]
);
return null;
}
$this->cleanTmpFiles();
//new bitmap image object
$image = new Image();
$image->loadFromData((string)$bp);
//check if image object is valid
return $image->valid() ? $image : null;
}
/**
* Returns a preview of maxX times maxY dimensions in JPG format
*
* * The default resolution is already 72dpi, no need to change it for a bitmap output
* * It's possible to have proper colour conversion using profileimage().
* ICC profiles are here: http://www.color.org/srgbprofiles.xalter
* * It's possible to Gamma-correct an image via gammaImage()
*
* @param string $tmpPath the location of the file to convert
* @param int $maxX
* @param int $maxY
*
* @return \Imagick
*
* @throws \Exception
*/
private function getResizedPreview($tmpPath, $maxX, $maxY) {
$bp = new \Imagick();
// Some files just contain (or at least are identified as) other formats
// like JPEG. We just need to check if the image is safe to process.
$bp->pingImage($this->formatHint() . ':' . $tmpPath . '[0]');
$mimeType = $bp->getImageMimeType();
if (!preg_match('/^image\/(x-)?(png|jpeg|gif|bmp|tiff|webp|hei(f|c)|avif)$/', $mimeType)) {
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
}
// Layer 0 contains either the bitmap or a flat representation of all vector layers
$bp->readImage($this->formatHint() . ':' . $tmpPath . '[0]');
// Fix orientation from EXIF
$bp->autoOrient();
$bp->setImageFormat('jpg');
$bp = $this->resize($bp, $maxX, $maxY);
return $bp;
}
/**
* Returns a resized \Imagick object
*
* If you want to know more on the various methods available to resize an
* image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
*
* @param \Imagick $bp
* @param int $maxX
* @param int $maxY
*
* @return \Imagick
*/
private function resize($bp, $maxX, $maxY) {
[$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
// We only need to resize a preview which doesn't fit in the maximum dimensions
if ($previewWidth > $maxX || $previewHeight > $maxY) {
// If we want a small image (thumbnail) let's be most space- and time-efficient
if ($maxX <= 500 && $maxY <= 500) {
$bp->thumbnailImage($maxY, $maxX, true);
$bp->stripImage();
} else {
// A bigger image calls for some better resizing algorithm
// According to http://www.imagemagick.org/Usage/filter/#lanczos
// the catrom filter is almost identical to Lanczos2, but according
// to https://www.php.net/manual/en/imagick.resizeimage.php it is
// significantly faster
$bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
}
}
return $bp;
}
}