From 270f84abe9e2c73b1492b4753150a2a3cbcbbab3 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 20 Dec 2022 19:17:43 +0900 Subject: [PATCH] Separate image rotation check into its own method in FileHandler --- classes/file/FileHandler.class.php | 32 ++++++++++++++++++++++++++++++ modules/file/file.controller.php | 24 +++++++--------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/classes/file/FileHandler.class.php b/classes/file/FileHandler.class.php index 54095c462..3b787017b 100644 --- a/classes/file/FileHandler.class.php +++ b/classes/file/FileHandler.class.php @@ -485,6 +485,38 @@ class FileHandler return true; } + /** + * Check if image needs rotation + * + * @param string $filename + * @return int|bool 0, 90, 180, 360, or false + */ + public static function checkImageRotation(string $filename) + { + if (function_exists('exif_read_data')) + { + $exif = @exif_read_data($filename); + if($exif && isset($exif['Orientation'])) + { + switch ($exif['Orientation']) + { + case 3: return 180; + case 6: return 270; + case 8: return 90; + default: return 0; + } + } + else + { + return false; + } + } + else + { + return false; + } + } + /** * Moves an image file (resizing is possible) * diff --git a/modules/file/file.controller.php b/modules/file/file.controller.php index 305626167..c35172ff8 100644 --- a/modules/file/file.controller.php +++ b/modules/file/file.controller.php @@ -1037,27 +1037,17 @@ class fileController extends file } // Adjust image rotation - if ($config->image_autorotate && $image_info['type'] === 'jpg' && function_exists('exif_read_data')) + if ($config->image_autorotate && $image_info['type'] === 'jpg') { - $exif = @exif_read_data($file_info['tmp_name']); - if($exif && isset($exif['Orientation'])) + $rotate = FileHandler::checkImageRotation($file_info['tmp_name']); + if ($rotate) { - switch ($exif['Orientation']) + if ($rotate === 90 || $rotate === 270) { - case 3: $rotate = 180; break; - case 6: $rotate = 270; break; - case 8: $rotate = 90; break; - default: $rotate = 0; - } - if ($rotate) - { - if ($rotate === 90 || $rotate === 270) - { - $adjusted['width'] = $image_info['height']; - $adjusted['height'] = $image_info['width']; - } - $adjusted['rotate'] = $rotate; + $adjusted['width'] = $image_info['height']; + $adjusted['height'] = $image_info['width']; } + $adjusted['rotate'] = $rotate; } }