From ccfeebec7e96bac06e01fb9388c133f2270c4ce8 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 15 Aug 2023 15:58:28 -0300 Subject: [PATCH] Disable auto rotate for HEIF images in imaginary. Issue #662 --- lib/Helper/Imaginary.php | 42 +++++++++++++++++++++++++--------------- lib/Helper/TempImage.php | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/Helper/Imaginary.php b/lib/Helper/Imaginary.php index 4cffaaed..edbf3aa9 100644 --- a/lib/Helper/Imaginary.php +++ b/lib/Helper/Imaginary.php @@ -71,17 +71,24 @@ public function getInfo(string $filepath): array { } $info = json_decode($response->getBody(), true); + + $type = $info['type']; + //NOTE: Imaginary has problems rorating heic images. Issue #662 + $autorotate = ($info['orientation'] > 4 && $type != 'heif'); + return [ + 'type' => $type, + 'autorotate' => $autorotate, // Rotates the size, since it is important and Imaginary do not do that. - 'width' => $info['orientation'] < 5 ? $info['width'] : $info['height'], - 'height' => $info['orientation'] < 5 ? $info['height'] : $info['width'] + 'width' => $autorotate ? $info['height'] : $info['width'], + 'height' => $autorotate ? $info['width'] : $info['height'] ]; } /** * @return string|resource Returns the resized image */ - public function getResized(string $filepath, int $width, int $height, string $mimeType) { + public function getResized(string $filepath, int $width, int $height, bool $autorotate, string $mimeType) { $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); $imaginaryUrl = rtrim($imaginaryUrl, '/'); @@ -96,20 +103,23 @@ public function getResized(string $filepath, int $width, int $height, string $mi $type = 'jpeg'; } - $operations = [ - [ + $operations = []; + + if ($autorotate) { + $operations[] = [ 'operation' => 'autorotate', - ], - [ - 'operation' => 'resize', - 'params' => [ - 'width' => $width, - 'height' => $height, - 'stripmeta' => 'true', - 'type' => $type, - 'norotation' => 'true', - 'force' => 'true' - ] + ]; + } + + $operations[] = [ + 'operation' => 'resize', + 'params' => [ + 'width' => $width, + 'height' => $height, + 'stripmeta' => 'true', + 'type' => $type, + 'norotation' => 'true', + 'force' => 'true' ] ]; diff --git a/lib/Helper/TempImage.php b/lib/Helper/TempImage.php index fce2d75e..8008ac0e 100644 --- a/lib/Helper/TempImage.php +++ b/lib/Helper/TempImage.php @@ -130,7 +130,7 @@ private function prepareImage() { $newWidth = intval(round($widthOrig * $scaleFactor)); $newHeight = intval(round($heightOrig * $scaleFactor)); - $resizedResource = $this->imaginary->getResized($this->imagePath, $newWidth, $newHeight, $this->preferredMimeType); + $resizedResource = $this->imaginary->getResized($this->imagePath, $newWidth, $newHeight, $fileInfo['autorotate'], $this->preferredMimeType); $this->loadFromData($resizedResource); if (!$this->valid()) {