Improved error handling for exif_read_data() File not supported

This commit is contained in:
djmaze 2021-10-28 21:32:59 +02:00
parent d2e69d7459
commit b326fb5af4
3 changed files with 36 additions and 9 deletions

View file

@ -0,0 +1,31 @@
<?php
namespace SnappyMail\Image;
class Exif
{
/*
const
ORIENTATION_UNDEFINED = 0,
ORIENTATION_TOPLEFT = 1,
ORIENTATION_TOPRIGHT = 2,
ORIENTATION_BOTTOMRIGHT = 3,
ORIENTATION_BOTTOMLEFT = 4,
ORIENTATION_LEFTTOP = 5,
ORIENTATION_RIGHTTOP = 6,
ORIENTATION_RIGHTBOTTOM = 7,
ORIENTATION_LEFTBOTTOM = 8;
*/
public static function getImageOrientation(string &$data, array $image_info = null) : int
{
$image_info = empty($image_info['mime']) ? \getimagesizefromstring($data) : $image_info;
if (!empty($image_info['mime']) && \is_callable('exif_read_data')) {
$exif = \exif_read_data('data://'.$image_info['mime'].';base64,' . \base64_encode($data));
if (false !== $exif) {
return \max(0, \intval($exif['IFD0.Orientation'] ?? 0));
}
\error_log("{$image_info['mime']} " . \error_get_last()['message']);
}
return 0;
}
}

View file

@ -13,7 +13,7 @@ class GD2 implements \SnappyMail\Image
$file, $file,
$format, $format,
$compression_q = 85, $compression_q = 85,
$orientation = 1, $orientation = 0,
$type; $type;
function __destruct() function __destruct()
@ -51,9 +51,7 @@ class GD2 implements \SnappyMail\Image
$gd2->file = 'blob'; $gd2->file = 'blob';
$gd2->type = (int) $imginfo[2]; $gd2->type = (int) $imginfo[2];
$gd2->format = $format; $gd2->format = $format;
if (\is_callable('exif_read_data') && $exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) { $gd2->orientation = Exif::getImageOrientation($data, $imginfo);
$gd2->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
}
return $gd2; return $gd2;
} }

View file

@ -7,7 +7,7 @@ if (!\class_exists('Gmagick',false)) { return; }
class GMagick extends \Gmagick implements \SnappyMail\Image class GMagick extends \Gmagick implements \SnappyMail\Image
{ {
private private
$orientation = 1; $orientation = 0;
function __destruct() function __destruct()
{ {
@ -22,10 +22,8 @@ class GMagick extends \Gmagick implements \SnappyMail\Image
} }
if (\method_exists($gmagick, 'getImageOrientation')) { if (\method_exists($gmagick, 'getImageOrientation')) {
$gmagick->orientation = $gmagick->getImageOrientation(); $gmagick->orientation = $gmagick->getImageOrientation();
} else if (\is_callable('exif_read_data') && $imginfo = \getimagesizefromstring($data)) { } else {
if ($exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) { $gd2->orientation = Exif::getImageOrientation($data);
$gmagick->orientation = \max(1, \intval($exif['IFD0.Orientation'] ?? 0));
}
} }
return $gmagick; return $gmagick;
} }