From 137bdb356aacc01409d22aee3f0904497616cf3c Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 29 Jun 2024 09:51:20 +0200 Subject: [PATCH] Avoid unnecessary exif_read_data calls (#1371) EXIF data extraction makes only sense for JPEG and TIFF format. This patch checks the format and calls exif_read_data only for appropriate formats. Previously, the function was also called with formats that can not contain EXIF data. This resulted in warnings. --- src/Drivers/Gd/Decoders/BinaryImageDecoder.php | 17 +++++++++-------- .../Gd/Decoders/FilePathImageDecoder.php | 6 ++++-- .../Imagick/Decoders/BinaryImageDecoder.php | 11 +++++++++-- .../Imagick/Decoders/FilePathImageDecoder.php | 6 ++++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Drivers/Gd/Decoders/BinaryImageDecoder.php b/src/Drivers/Gd/Decoders/BinaryImageDecoder.php index 5eddf07f5..5d30acccd 100644 --- a/src/Drivers/Gd/Decoders/BinaryImageDecoder.php +++ b/src/Drivers/Gd/Decoders/BinaryImageDecoder.php @@ -9,6 +9,7 @@ use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Exceptions\DecoderException; +use Intervention\Image\Format; use Intervention\Image\Modifiers\AlignRotationModifier; class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface @@ -48,17 +49,17 @@ private function decodeBinary(string $input): ImageInterface // create image instance $image = parent::decode($gd); - // extract & set exif data - $image->setExif($this->extractExifData($input)); + // get media type + $mediaType = $this->getMediaTypeByBinary($input); - try { - // set mediaType on origin - $image->origin()->setMediaType( - $this->getMediaTypeByBinary($input) - ); - } catch (DecoderException) { + // extract & set exif data for appropriate formats + if (in_array($mediaType->format(), [Format::JPEG, Format::TIFF])) { + $image->setExif($this->extractExifData($input)); } + // set mediaType on origin + $image->origin()->setMediaType($mediaType); + // adjust image orientation if ($this->driver()->config()->autoOrientation) { $image->modify(new AlignRotationModifier()); diff --git a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php index acba9df9a..59b984f7a 100644 --- a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php @@ -40,8 +40,10 @@ public function decode(mixed $input): ImageInterface|ColorInterface $image->origin()->setFilePath($input); $image->origin()->setMediaType($mediaType); - // extract exif - $image->setExif($this->extractExifData($input)); + // extract exif for the appropriate formats + if ($mediaType->format() === Format::JPEG) { + $image->setExif($this->extractExifData($input)); + } // adjust image orientation if ($this->driver()->config()->autoOrientation) { diff --git a/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php b/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php index 177a36e63..8e4ac0bd1 100644 --- a/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php +++ b/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php @@ -7,8 +7,10 @@ use Imagick; use ImagickException; use Intervention\Image\Exceptions\DecoderException; +use Intervention\Image\Format; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; +use Intervention\Image\MediaType; class BinaryImageDecoder extends NativeObjectDecoder { @@ -28,8 +30,13 @@ public function decode(mixed $input): ImageInterface|ColorInterface // decode image $image = parent::decode($imagick); - // extract exif data - $image->setExif($this->extractExifData($input)); + // get media type enum from string media type + $mediaType = MediaType::from($image->origin()->mediaType()); + + // extract exif data for appropriate formats + if (in_array($mediaType->format(), [Format::JPEG, Format::TIFF])) { + $image->setExif($this->extractExifData($input)); + } return $image; } diff --git a/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php b/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php index 639c0c88d..4a8d4e119 100644 --- a/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php @@ -31,8 +31,10 @@ public function decode(mixed $input): ImageInterface|ColorInterface // set file path on origin $image->origin()->setFilePath($input); - // extract exif data - $image->setExif($this->extractExifData($input)); + // extract exif data for the appropriate formats + if (in_array($imagick->getImageFormat(), ['JPEG', 'TIFF', 'TIF'])) { + $image->setExif($this->extractExifData($input)); + } return $image; }