Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 82 additions & 29 deletions Sources/Graphics/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************/
Expand Down Expand Up @@ -477,14 +482,33 @@ 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;

// 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)) {
$attempted_imagick = true;

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 ($attempted_imagick && $destination !== $this->source && is_file($destination)) {
@unlink($destination);
}

try {
$success = $this->resizeUsingGD($destination, $max_width, $max_height, $preferred_type);
} catch (\Throwable $e) {
$success = false;
}
}

if (!$success) {
Expand Down Expand Up @@ -566,24 +590,46 @@ 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;
}
}
}

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.
*
Expand Down Expand Up @@ -772,21 +818,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.
}
}

Expand Down Expand Up @@ -1078,10 +1126,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)) {
Expand All @@ -1105,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...
Expand Down