diff --git a/inc/Service/Media/LibMagickMediaHandler.php b/inc/Service/Media/LibMagickMediaHandler.php index 054416bb..14b3b076 100644 --- a/inc/Service/Media/LibMagickMediaHandler.php +++ b/inc/Service/Media/LibMagickMediaHandler.php @@ -67,8 +67,7 @@ class LibMagickMediaHandler implements MediaHandler { /** * @return bool Returns if width and height were swapped. */ - private static function adjustOrientation(\Imagick $imagick) { - $orientation = $imagick->getImageOrientation(); + private static function adjustOrientation(\Imagick $imagick, int $orientation): void { $degrees = self::degreesFromOrientation($orientation); $flipped = self::isFlippedFromOrientation($orientation); @@ -81,7 +80,6 @@ class LibMagickMediaHandler implements MediaHandler { if ($flipped) { $imagick->flopImage(); } - return $degrees !== 0 && $degrees !== 180; } private function generateThumbImpl( @@ -219,15 +217,23 @@ class LibMagickMediaHandler implements MediaHandler { // Open it as the supplied mime type. $imagick = new \Imagick("$ext:$path"); - $width = $imagick->getImageWidth(); - $height = $imagick->getImageHeight(); + $orientation = $imagick->getImageOrientation(); - if ($width > $this->image_max_width || $height > $this->image_max_height) { + if (self::isFlippedFromOrientation($orientation)) { + $width = $imagick->getImageHeight(); + $height = $imagick->getImageWidth(); + } else { + $width = $imagick->getImageWidth(); + $height = $imagick->getImageHeight(); + } + + if ($width > $this->image_max_width && $height > $this->image_max_height) { + // Too big even if rotated. $imagick->clear(); 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) { @@ -241,10 +247,7 @@ class LibMagickMediaHandler implements MediaHandler { int $max_width, int $max_height ): ThumbGenerationResult { - list($imagick, $source_file_path, $source_file_mime, $source_file_kind) = $handle; - - $width = $imagick->getImageWidth(); - $height = $imagick->getImageHeight(); + list($imagick, $source_file_path, $source_file_mime, $source_file_kind, $width, $height, $orientation) = $handle; if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) { $out_path = $preferred_out_file_basepath . '.' . Metadata\mime_to_ext($source_file_mime); @@ -258,12 +261,7 @@ class LibMagickMediaHandler implements MediaHandler { $max_height ); } else { - $swap = self::adjustOrientation($imagick); - if ($swap) { - $tmp = $width; - $width = $height; - $height = $tmp; - } + self::adjustOrientation($imagick, $orientation); return self::generateThumbImpl( $imagick, @@ -286,10 +284,7 @@ class LibMagickMediaHandler implements MediaHandler { int $thumb_max_width, int $thumb_max_height ): MediaInstallResult { - list($imagick, $media_file_path, $media_file_mime, $media_file_kind) = $handle; - - $width = $imagick->getImageWidth(); - $height = $imagick->getImageHeight(); + list($imagick, $media_file_path, $media_file_mime, $media_file_kind, $width, $height, $orientation) = $handle; if (!$this->strip_metadata) { $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); } else { - $swap = self::adjustOrientation($imagick); - if ($swap) { - $tmp = $width; - $width = $height; - $height = $tmp; - } + self::adjustOrientation($imagick, $orientation); $thumb = self::generateThumbImpl( $imagick, @@ -324,12 +314,7 @@ class LibMagickMediaHandler implements MediaHandler { } return new MediaInstallResult($thumb, $media_out_path); } else { - $swap = self::adjustOrientation($imagick); - if ($swap) { - $tmp = $width; - $width = $height; - $height = $tmp; - } + self::adjustOrientation($imagick, $orientation); // Backup the color profile, then re-apply it. $profiles = $imagick->getImageProfiles('icc', true);