From 129a60c48c850e38e606fa421f8beb5bc1d3e542 Mon Sep 17 00:00:00 2001 From: boredskull Date: Mon, 23 Nov 2026 12:37:05 +0000 Subject: [PATCH 1/3] Fix unsupported Imagick formats with GD fallback (#6411) --- Sources/Graphics/Image.php | 103 +++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/Sources/Graphics/Image.php b/Sources/Graphics/Image.php index 0fee0f8437..09caf4f2df 100644 --- a/Sources/Graphics/Image.php +++ b/Sources/Graphics/Image.php @@ -189,6 +189,11 @@ class Image */ protected static array $supported; + /** + * Cache of available ImageMagick formats, or null until queried. + */ + protected static ?array $imagick_formats = null; + /**************** * Public methods ****************/ @@ -478,13 +483,29 @@ public function resize(string $destination, int $max_width, int $max_height, int $max_height = (int) round($max_height); $success = false; - // Do the job using ImageMagick. - if (\extension_loaded('imagick') && isset(self::IMAGETYPE_TO_IMAGICK[$preferred_type])) { - $success = $this->resizeUsingImagick($destination, $max_width, $max_height, $preferred_type); + // Prefer ImageMagick when it supports both formats, but let GD + // handle formats missing from the installed ImageMagick delegates. + if (self::imagickSupportsFormat($this->type) && self::imagickSupportsFormat($preferred_type)) { + try { + $success = $this->resizeUsingImagick($destination, $max_width, $max_height, $preferred_type); + } catch (\Throwable $e) { + $success = false; + } } - // Do the job using GD. - elseif (\extension_loaded('gd')) { - $success = $this->resizeUsingGD($destination, $max_width, $max_height, $preferred_type); + + // ImageMagick can advertise a format yet still fail to process a file. + // In that case, retry with GD if it supports both formats. + if (!$success && self::gdSupportsFormat($this->type) && self::gdSupportsFormat($preferred_type)) { + // Remove any partial ImageMagick output before retrying. + if (is_file($destination)) { + @unlink($destination); + } + + try { + $success = $this->resizeUsingGD($destination, $max_width, $max_height, $preferred_type); + } catch (\Throwable $e) { + $success = false; + } } if (!$success) { @@ -566,17 +587,9 @@ public static function getSupportedFormats(): array if (!isset(self::$supported)) { self::$supported = []; - if (\extension_loaded('imagick')) { - foreach (self::getImageTypes() as $name => $int) { - if (isset(self::IMAGETYPE_TO_IMAGICK[$int])) { - self::$supported[$name] = $int; - } - } - } elseif (\extension_loaded('gd')) { - foreach (self::getImageTypes() as $name => $int) { - if (imagetypes() & (self::IMAGETYPE_TO_IMG[$int] ?? 0)) { - self::$supported[$name] = $int; - } + foreach (self::getImageTypes() as $name => $int) { + if (self::imagickSupportsFormat($int) || self::gdSupportsFormat($int)) { + self::$supported[$name] = $int; } } } @@ -584,6 +597,36 @@ public static function getSupportedFormats(): array return self::$supported; } + /** + * Check whether the installed ImageMagick delegates support a format. + */ + protected static function imagickSupportsFormat(int $type): bool + { + if (!\extension_loaded('imagick') || !isset(self::IMAGETYPE_TO_IMAGICK[$type])) { + return false; + } + + if (self::$imagick_formats === null) { + try { + self::$imagick_formats = array_map('strtolower', \Imagick::queryFormats()); + } catch (\Throwable $e) { + self::$imagick_formats = []; + } + } + + return \in_array(self::IMAGETYPE_TO_IMAGICK[$type], self::$imagick_formats, true); + } + + /** + * Check whether GD has format-specific support in this PHP installation. + */ + protected static function gdSupportsFormat(int $type): bool + { + return \extension_loaded('gd') + && isset(self::IMAGETYPE_TO_IMG[$type]) + && (imagetypes() & self::IMAGETYPE_TO_IMG[$type]) !== 0; + } + /** * Check whether we have enough memory to make a thumbnail. * @@ -772,21 +815,23 @@ protected function getDimensionsAndOrientation(): void } } - // Next try ImageMagick. + // Next try ImageMagick. The constructor itself can fail when the + // installed ImageMagick build lacks a decoder for this format. if (\extension_loaded('imagick')) { - $imagick = new \Imagick($this->source); - try { - $this->orientation = $imagick->getImageOrientation(); - } catch (\Throwable $e) { - } + $imagick = new \Imagick($this->source); + + try { + $this->orientation = $imagick->getImageOrientation(); + } catch (\Throwable $e) { + } - try { $this->width = $imagick->getImageWidth(); $this->height = $imagick->getImageHeight(); return; } catch (\Throwable $e) { + // Fall through to getimagesize() below. } } @@ -1078,10 +1123,14 @@ protected function resizeUsingGD(string $destination, int $max_width, int $max_h return true; } - // Figure out the functions we need. - $imagecreatefrom = 'imagecreatefrom' . strtolower(substr(array_search($this->type, self::getSupportedFormats()), 10)); + // The combined supported-format list also contains Imagick-only + // formats, so check GD itself before constructing GD function names. + if (!self::gdSupportsFormat($this->type) || !self::gdSupportsFormat($preferred_type)) { + return false; + } - $imagesave = 'image' . strtolower(substr(array_search($preferred_type, self::getSupportedFormats()), 10)); + $imagecreatefrom = 'imagecreatefrom' . image_type_to_extension($this->type, false); + $imagesave = 'image' . image_type_to_extension($preferred_type, false); // Do the functions exist? if (!\function_exists($imagecreatefrom) || !\function_exists($imagesave)) { From b049aeb179d560886dcb72aaf030fec2e797c0b8 Mon Sep 17 00:00:00 2001 From: boredskull Date: Mon, 23 Nov 2026 12:37:32 +0000 Subject: [PATCH 2/3] Protect GD retry output and preserve dimensions on re-encode --- Sources/Graphics/Image.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/Graphics/Image.php b/Sources/Graphics/Image.php index 09caf4f2df..164ae5f85b 100644 --- a/Sources/Graphics/Image.php +++ b/Sources/Graphics/Image.php @@ -482,10 +482,13 @@ public function resize(string $destination, int $max_width, int $max_height, int $max_width = (int) round($max_width); $max_height = (int) round($max_height); $success = false; + $attempted_imagick = false; // Prefer ImageMagick when it supports both formats, but let GD // handle formats missing from the installed ImageMagick delegates. if (self::imagickSupportsFormat($this->type) && self::imagickSupportsFormat($preferred_type)) { + $attempted_imagick = true; + try { $success = $this->resizeUsingImagick($destination, $max_width, $max_height, $preferred_type); } catch (\Throwable $e) { @@ -497,7 +500,7 @@ public function resize(string $destination, int $max_width, int $max_height, int // In that case, retry with GD if it supports both formats. if (!$success && self::gdSupportsFormat($this->type) && self::gdSupportsFormat($preferred_type)) { // Remove any partial ImageMagick output before retrying. - if (is_file($destination)) { + if ($attempted_imagick && is_file($destination)) { @unlink($destination); } @@ -1154,8 +1157,9 @@ protected function resizeUsingGD(string $destination, int $max_width, int $max_h $dst_width = (int) round($this->width * $max_height / $this->height); $dst_height = (int) $max_height; } else { - $dst_width = 0; - $dst_height = 0; + // Re-encoding with no size limits must preserve the original size. + $dst_width = (int) $this->width; + $dst_height = (int) $this->height; } // Don't bother resizing if it's already smaller... From d321ee98fe56be99bc71d43103352bea76523572 Mon Sep 17 00:00:00 2001 From: boredskull Date: Mon, 23 Nov 2026 12:38:03 +0000 Subject: [PATCH 3/3] Guard original image while cleaning partial Imagick output --- Sources/Graphics/Image.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Graphics/Image.php b/Sources/Graphics/Image.php index 164ae5f85b..480019dec6 100644 --- a/Sources/Graphics/Image.php +++ b/Sources/Graphics/Image.php @@ -500,7 +500,7 @@ public function resize(string $destination, int $max_width, int $max_height, int // In that case, retry with GD if it supports both formats. if (!$success && self::gdSupportsFormat($this->type) && self::gdSupportsFormat($preferred_type)) { // Remove any partial ImageMagick output before retrying. - if ($attempted_imagick && is_file($destination)) { + if ($attempted_imagick && $destination !== $this->source && is_file($destination)) { @unlink($destination); }