Improved image processor

This commit is contained in:
djmaze 2021-04-07 14:42:38 +02:00
parent 95d5e10268
commit d99b15251f
5 changed files with 57 additions and 31 deletions

View file

@ -246,12 +246,11 @@ trait Raw
try try
{ {
$oImage = static::loadImage(\stream_get_contents($rResource), $bDetectImageOrientation, 60); $oImage = static::loadImage(\stream_get_contents($rResource), $bDetectImageOrientation, 60);
$oImage->setImageFormat('png');
// $oImage->setImageFormat('webp');
\header('Content-Disposition: inline; '. \header('Content-Disposition: inline; '.
\trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut.'_thumb60x60.png')), true); \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut.'_thumb60x60.png')), true);
\header('Content-Type: '.$oImage->getImageMimeType()); $oImage->show('png');
echo $oImage->getImageBlob(); // $oImage->show('webp'); // Little Britain: "Safari says NO"
exit;
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
@ -267,8 +266,7 @@ trait Raw
$oImage = static::loadImage($sLoadedData, $bDetectImageOrientation); $oImage = static::loadImage($sLoadedData, $bDetectImageOrientation);
\header('Content-Disposition: inline; '. \header('Content-Disposition: inline; '.
\trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true); \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true);
\header('Content-Type: '.$oImage->getImageMimeType()); $oImage->show();
echo $oImage->getImageBlob();
} }
catch (\Throwable $oException) catch (\Throwable $oException)
{ {
@ -363,7 +361,7 @@ trait Raw
// rotateImageByOrientation // rotateImageByOrientation
if ($bDetectImageOrientation) { if ($bDetectImageOrientation) {
switch ($oImage->getImageOrientation()) switch ($oImage->getOrientation())
{ {
case 2: // flip horizontal case 2: // flip horizontal
$oImage->flopImage(); $oImage->flopImage();

View file

@ -6,5 +6,9 @@ interface Image
{ {
public static function createFromString(string $data); public static function createFromString(string $data);
public function getOrientation() : int;
public function rotate(float $degrees) : bool; public function rotate(float $degrees) : bool;
public function show(?string $format = null) : void;
} }

View file

@ -51,21 +51,30 @@ 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')) { if (\is_callable('exif_read_data') && $exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) {
$exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data)); $gd2->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
if ($exif) {
$gd2->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
}
} }
return $gd2; return $gd2;
} }
public function getOrientation() : int
{
return $this->orientation;
}
public function rotate(float $degrees) : bool public function rotate(float $degrees) : bool
{ {
return $this->rotateImage(0, $degrees); return $this->rotateImage(0, $degrees);
} }
private function store_image($filename) : bool public function show(?string $format = null) : void
{
$format && $this->setImageFormat($format);
\header('Content-Type: ' . $this->getImageMimeType());
$this->store_image(null);
}
private function store_image(?string $filename) : bool
{ {
switch ($this->format) switch ($this->format)
{ {
@ -179,7 +188,7 @@ class GD2 implements \SnappyMail\Image
return \ob_get_clean(); return \ob_get_clean();
} }
public function getImageMimeType() public function getImageMimeType() : string
{ {
switch ($this->format) switch ($this->format)
{ {
@ -196,12 +205,7 @@ class GD2 implements \SnappyMail\Image
case 'webp': case 'webp':
return 'image/webp'; return 'image/webp';
} }
return false; return 'application/octet-stream';
}
public function getImageOrientation() : int
{
return $this->orientation;
} }
public function rotateImage($background, $degrees) public function rotateImage($background, $degrees)

View file

@ -20,21 +20,34 @@ class GMagick extends \Gmagick implements \SnappyMail\Image
if (!$gmagick->readimageblob($data)) { if (!$gmagick->readimageblob($data)) {
throw new \InvalidArgumentException('Failed to load image'); throw new \InvalidArgumentException('Failed to load image');
} }
if (\is_callable('exif_read_data')) { if (\method_exists($gmagick, 'getImageOrientation')) {
$exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data)); $gmagick->orientation = $gmagick->getImageOrientation();
if ($exif) { } else if (\is_callable('exif_read_data') && $imginfo = \getimagesizefromstring($data)) {
$gmagick->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0)); if ($exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) {
$gmagick->orientation = \max(1, \intval($exif['IFD0.Orientation'] ?? 0));
} }
} }
return $gmagick; return $gmagick;
} }
public function getOrientation() : int
{
return $this->orientation;
}
public function rotate(float $degrees) : bool public function rotate(float $degrees) : bool
{ {
return $this->rotateImage(new \GmagickPixel(), $degrees); return $this->rotateImage(new \GmagickPixel(), $degrees);
} }
public function getImageMimeType() public function show(?string $format = null) : void
{
$format && $this->setImageFormat($format);
\header('Content-Type: ' . $this->getImageMimeType());
echo $this;
}
public function getImageMimeType() : string
{ {
switch (\strtolower(parent::getImageFormat())) switch (\strtolower(parent::getImageFormat()))
{ {
@ -50,11 +63,6 @@ class GMagick extends \Gmagick implements \SnappyMail\Image
case 'webp': case 'webp':
return 'image/webp'; return 'image/webp';
} }
return false; return 'application/octet-stream';
}
public function getImageOrientation() : int
{
return $this->orientation;
} }
} }

View file

@ -21,8 +21,20 @@ class IMagick extends \Imagick implements \SnappyMail\Image
return $imagick; return $imagick;
} }
public function getOrientation() : int
{
return $this->getImageOrientation();
}
public function rotate(float $degrees) : bool public function rotate(float $degrees) : bool
{ {
return $this->rotateImage(new \ImagickPixel(), $degrees); return $this->rotateImage(new \ImagickPixel(), $degrees);
} }
public function show(?string $format = null) : void
{
$format && $this->setImageFormat($format);
\header('Content-Type: ' . $this->getImageMimeType());
echo $this;
}
} }