Separate image rotation check into its own method in FileHandler

This commit is contained in:
Kijin Sung 2022-12-20 19:17:43 +09:00
parent 42baab1c2c
commit 270f84abe9
2 changed files with 39 additions and 17 deletions

View file

@ -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)
*

View file

@ -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;
}
}