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

View file

@ -6,5 +6,9 @@ interface Image
{
public static function createFromString(string $data);
public function getOrientation() : int;
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->type = (int) $imginfo[2];
$gd2->format = $format;
if (\is_callable('exif_read_data')) {
$exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data));
if ($exif) {
$gd2->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
}
if (\is_callable('exif_read_data') && $exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) {
$gd2->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
}
return $gd2;
}
public function getOrientation() : int
{
return $this->orientation;
}
public function rotate(float $degrees) : bool
{
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)
{
@ -179,7 +188,7 @@ class GD2 implements \SnappyMail\Image
return \ob_get_clean();
}
public function getImageMimeType()
public function getImageMimeType() : string
{
switch ($this->format)
{
@ -196,12 +205,7 @@ class GD2 implements \SnappyMail\Image
case 'webp':
return 'image/webp';
}
return false;
}
public function getImageOrientation() : int
{
return $this->orientation;
return 'application/octet-stream';
}
public function rotateImage($background, $degrees)

View file

@ -20,21 +20,34 @@ class GMagick extends \Gmagick implements \SnappyMail\Image
if (!$gmagick->readimageblob($data)) {
throw new \InvalidArgumentException('Failed to load image');
}
if (\is_callable('exif_read_data')) {
$exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data));
if ($exif) {
$gmagick->orientation = \max(1, \intval($oMetadata['IFD0.Orientation'] ?? 0));
if (\method_exists($gmagick, 'getImageOrientation')) {
$gmagick->orientation = $gmagick->getImageOrientation();
} else if (\is_callable('exif_read_data') && $imginfo = \getimagesizefromstring($data)) {
if ($exif = \exif_read_data('data://'.$imginfo['mime'].';base64,' . \base64_encode($data))) {
$gmagick->orientation = \max(1, \intval($exif['IFD0.Orientation'] ?? 0));
}
}
return $gmagick;
}
public function getOrientation() : int
{
return $this->orientation;
}
public function rotate(float $degrees) : bool
{
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()))
{
@ -50,11 +63,6 @@ class GMagick extends \Gmagick implements \SnappyMail\Image
case 'webp':
return 'image/webp';
}
return false;
}
public function getImageOrientation() : int
{
return $this->orientation;
return 'application/octet-stream';
}
}

View file

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