diff --git a/src/Drivers/Gd/Decoders/AbstractDecoder.php b/src/Drivers/Gd/Decoders/AbstractDecoder.php new file mode 100644 index 00000000..287faa6f --- /dev/null +++ b/src/Drivers/Gd/Decoders/AbstractDecoder.php @@ -0,0 +1,53 @@ +extractExifData($input) ); - // set mediaType on origin - if ($info = getimagesizefromstring($input)) { - $image->origin()->setMediaType($info['mime']); + try { + // set mediaType on origin + $image->origin()->setMediaType( + $this->getMediaTypeByBinary($input) + ); + } catch (DecoderException) { } // adjust image orientation diff --git a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php index dfb36e64..c88d636e 100644 --- a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php @@ -37,8 +37,9 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface // detect media (mime) type $mediaType = $this->getMediaTypeByFilePath($input); - // gif files might be animated and therefore cannot be handled by the standard GD decoder. $image = match ($mediaType) { + // gif files might be animated and therefore cannot + // be handled by the standard GD decoder. 'image/gif' => $this->decodeGif($input), default => parent::decode(match ($mediaType) { 'image/jpeg', 'image/jpg', 'image/pjpeg' => imagecreatefromjpeg($input), @@ -69,25 +70,4 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface return $image; } - - /** - * Return media (mime) type of the file at given file path - * - * @param string $filepath - * @return string - */ - private function getMediaTypeByFilePath(string $filepath): string - { - $info = getimagesize($filepath); - - if (!is_array($info)) { - throw new DecoderException('Unable to decode input'); - } - - if (!array_key_exists('mime', $info)) { - throw new DecoderException('Unable to decode input'); - } - - return $info['mime']; - } } diff --git a/src/Drivers/Gd/Decoders/GdImageDecoder.php b/src/Drivers/Gd/Decoders/GdImageDecoder.php index c812b579..d48b4659 100644 --- a/src/Drivers/Gd/Decoders/GdImageDecoder.php +++ b/src/Drivers/Gd/Decoders/GdImageDecoder.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Gd\Decoders; use GdImage; -use Intervention\Image\Drivers\AbstractDecoder; use Intervention\Image\Drivers\Gd\Core; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Drivers\Gd\Frame; diff --git a/tests/Drivers/Gd/Decoders/AbstractDecoderTest.php b/tests/Drivers/Gd/Decoders/AbstractDecoderTest.php new file mode 100644 index 00000000..a77c3dd3 --- /dev/null +++ b/tests/Drivers/Gd/Decoders/AbstractDecoderTest.php @@ -0,0 +1,24 @@ +makePartial(); + $this->assertEquals('image/jpeg', $decoder->getMediaTypeByFilePath($this->getTestImagePath('test.jpg'))); + } + + public function testGetMediaTypeFromFileBinary(): void + { + $decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); + $this->assertEquals('image/jpeg', $decoder->getMediaTypeByBinary($this->getTestImageData('test.jpg'))); + } +}