From 1069cfb57043ab4c9f202157488a85aca61289ee Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 17 Feb 2024 10:46:48 +0000 Subject: [PATCH] Fix unhandled image detection --- src/Module/Photo.php | 6 +++--- src/Module/Proxy.php | 4 ++-- src/Object/Image.php | 2 +- src/Util/Images.php | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 3a064c48ac..1bac70cdfd 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -207,13 +207,13 @@ class Photo extends BaseApi $mimetype = $img->getType(); } - // if customsize is set and image is not a gif, resize it - if ($photo['type'] !== image_type_to_mime_type(IMAGETYPE_GIF) && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { + // if customsize is set and image resizing is supported for this image, resize it + if (Images::canResize($photo['type']) && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { $img = new Image($imgdata, $photo['type'], $photo['filename']); $img->scaleToSquare($customsize); $imgdata = $img->asString(); $mimetype = $img->getType(); - } elseif ($photo['type'] !== image_type_to_mime_type(IMAGETYPE_GIF) && $customsize > 0) { + } elseif (Images::canResize($photo['type']) && $customsize > 0) { $img = new Image($imgdata, $photo['type'], $photo['filename']); $img->scaleDown($customsize); $imgdata = $img->asString(); diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 12f58f3a84..8a72e40ea2 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -106,8 +106,8 @@ class Proxy extends BaseModule // stop. } - // reduce quality - if it isn't a GIF - if ($image->getImageType() != IMAGETYPE_GIF) { + // reduce quality - if it is supported for this image type + if (Images::canResize($image->getType())) { $image->scaleDown($request['size']); } diff --git a/src/Object/Image.php b/src/Object/Image.php index d938643751..e2be2dcfd3 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -66,7 +66,7 @@ class Image if (Images::isSupportedMimeType($type)) { $this->imageType = Images::getImageTypeByMimeType($type); - } elseif (($type == '') || substr($type, 0, 6) != 'image/' || substr($type, 0, 12) != ' application/') { + } elseif (($type == '') || substr($type, 0, 6) == 'image/' || substr($type, 0, 12) == ' application/') { $this->imageType = IMAGETYPE_WEBP; DI::logger()->debug('Unhandled image mime type, use WebP instead', ['type' => $type, 'filename' => $filename, 'size' => strlen($data)]); } else { diff --git a/src/Util/Images.php b/src/Util/Images.php index 33bae87a79..f5a7e5af3d 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -183,6 +183,21 @@ class Images return $types; } + /** + * Checks if the provided mime type can be handled for resizing. + * Only with Imagick installed, animated GIF and WebP keep their animation after resize. + * + * @param string $mimetype + * @return boolean + */ + public static function canResize(string $mimetype): bool + { + if (in_array(self::getImageTypeByMimeType($mimetype), [IMAGETYPE_GIF, IMAGETYPE_WEBP])) { + return class_exists('Imagick'); + } + return true; + } + /** * Fetch image mimetype from the image data or guessing from the file name *