Skip to content

Commit

Permalink
Avoid unnecessary exif_read_data calls (#1371)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
olivervogel authored Jun 29, 2024
1 parent c7fb60e commit 137bdb3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
17 changes: 9 additions & 8 deletions src/Drivers/Gd/Decoders/BinaryImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions src/Drivers/Gd/Decoders/FilePathImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions src/Drivers/Imagick/Decoders/BinaryImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Drivers/Imagick/Decoders/FilePathImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 137bdb3

Please sign in to comment.