LibMagickMediaHandler.php: handle rotated image sizes limits

This commit is contained in:
Zankaria 2025-03-28 12:41:06 +01:00
parent c4b766f133
commit 3e6c97e25a

View file

@ -67,8 +67,7 @@ class LibMagickMediaHandler implements MediaHandler {
/** /**
* @return bool Returns if width and height were swapped. * @return bool Returns if width and height were swapped.
*/ */
private static function adjustOrientation(\Imagick $imagick) { private static function adjustOrientation(\Imagick $imagick, int $orientation): void {
$orientation = $imagick->getImageOrientation();
$degrees = self::degreesFromOrientation($orientation); $degrees = self::degreesFromOrientation($orientation);
$flipped = self::isFlippedFromOrientation($orientation); $flipped = self::isFlippedFromOrientation($orientation);
@ -81,7 +80,6 @@ class LibMagickMediaHandler implements MediaHandler {
if ($flipped) { if ($flipped) {
$imagick->flopImage(); $imagick->flopImage();
} }
return $degrees !== 0 && $degrees !== 180;
} }
private function generateThumbImpl( private function generateThumbImpl(
@ -219,15 +217,23 @@ class LibMagickMediaHandler implements MediaHandler {
// Open it as the supplied mime type. // Open it as the supplied mime type.
$imagick = new \Imagick("$ext:$path"); $imagick = new \Imagick("$ext:$path");
$orientation = $imagick->getImageOrientation();
if (self::isFlippedFromOrientation($orientation)) {
$width = $imagick->getImageHeight();
$height = $imagick->getImageWidth();
} else {
$width = $imagick->getImageWidth(); $width = $imagick->getImageWidth();
$height = $imagick->getImageHeight(); $height = $imagick->getImageHeight();
}
if ($width > $this->image_max_width || $height > $this->image_max_height) { if ($width > $this->image_max_width && $height > $this->image_max_height) {
// Too big even if rotated.
$imagick->clear(); $imagick->clear();
throw new MediaException("Image too big", MediaException::ERR_IMAGE_TOO_LARGE); throw new MediaException("Image too big", MediaException::ERR_IMAGE_TOO_LARGE);
} }
return [ $imagick, $file_path, $file_mime, $file_kind ]; return [ $imagick, $file_path, $file_mime, $file_kind, $width, $height, $orientation ];
} }
public function closeHandle(mixed $handle) { public function closeHandle(mixed $handle) {
@ -241,10 +247,7 @@ class LibMagickMediaHandler implements MediaHandler {
int $max_width, int $max_width,
int $max_height int $max_height
): ThumbGenerationResult { ): ThumbGenerationResult {
list($imagick, $source_file_path, $source_file_mime, $source_file_kind) = $handle; list($imagick, $source_file_path, $source_file_mime, $source_file_kind, $width, $height, $orientation) = $handle;
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) { if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) {
$out_path = $preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime); $out_path = $preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime);
@ -258,12 +261,7 @@ class LibMagickMediaHandler implements MediaHandler {
$max_height $max_height
); );
} else { } else {
$swap = self::adjustOrientation($imagick); self::adjustOrientation($imagick, $orientation);
if ($swap) {
$tmp = $width;
$width = $height;
$height = $tmp;
}
return self::generateThumbImpl( return self::generateThumbImpl(
$imagick, $imagick,
@ -286,10 +284,7 @@ class LibMagickMediaHandler implements MediaHandler {
int $thumb_max_width, int $thumb_max_width,
int $thumb_max_height int $thumb_max_height
): MediaInstallResult { ): MediaInstallResult {
list($imagick, $media_file_path, $media_file_mime, $media_file_kind) = $handle; list($imagick, $media_file_path, $media_file_mime, $media_file_kind, $width, $height, $orientation) = $handle;
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if (!$this->strip_metadata) { if (!$this->strip_metadata) {
$media_out_path = $media_preferred_out_file_basepath . '.' . Metadata\mime_to_ext($media_file_mime); $media_out_path = $media_preferred_out_file_basepath . '.' . Metadata\mime_to_ext($media_file_mime);
@ -304,12 +299,7 @@ class LibMagickMediaHandler implements MediaHandler {
$thumb = new ThumbGenerationResult($thumb_out_path, $media_file_mime, $width, $height); $thumb = new ThumbGenerationResult($thumb_out_path, $media_file_mime, $width, $height);
} else { } else {
$swap = self::adjustOrientation($imagick); self::adjustOrientation($imagick, $orientation);
if ($swap) {
$tmp = $width;
$width = $height;
$height = $tmp;
}
$thumb = self::generateThumbImpl( $thumb = self::generateThumbImpl(
$imagick, $imagick,
@ -324,12 +314,7 @@ class LibMagickMediaHandler implements MediaHandler {
} }
return new MediaInstallResult($thumb, $media_out_path); return new MediaInstallResult($thumb, $media_out_path);
} else { } else {
$swap = self::adjustOrientation($imagick); self::adjustOrientation($imagick, $orientation);
if ($swap) {
$tmp = $width;
$width = $height;
$height = $tmp;
}
// Backup the color profile, then re-apply it. // Backup the color profile, then re-apply it.
$profiles = $imagick->getImageProfiles('icc', true); $profiles = $imagick->getImageProfiles('icc', true);