From bbf0f9f821e4cc78c5c3ffb6e8180a2848766821 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 26 Jun 2024 17:13:30 +0200 Subject: [PATCH] Change signature of internal methods The following internal methods now return enum MediaType instead of string. Intervention\Image\AbstractDecoder::getMediaTypeByFilePath() Intervention\Image\AbstractDecoder::getMediaTypeByBinary() --- src/Drivers/Gd/Decoders/AbstractDecoder.php | 13 +++++----- .../Gd/Decoders/FilePathImageDecoder.php | 24 +++++++------------ src/Origin.php | 9 ++++--- .../Gd/Decoders/AbstractDecoderTest.php | 11 +++++++-- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Drivers/Gd/Decoders/AbstractDecoder.php b/src/Drivers/Gd/Decoders/AbstractDecoder.php index 5003950d..11781f77 100644 --- a/src/Drivers/Gd/Decoders/AbstractDecoder.php +++ b/src/Drivers/Gd/Decoders/AbstractDecoder.php @@ -7,6 +7,7 @@ namespace Intervention\Image\Drivers\Gd\Decoders; use Intervention\Image\Drivers\SpecializableDecoder; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\SpecializedInterface; +use Intervention\Image\MediaType; abstract class AbstractDecoder extends SpecializableDecoder implements SpecializedInterface { @@ -15,9 +16,9 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ * * @param string $filepath * @throws DecoderException - * @return string + * @return MediaType */ - protected function getMediaTypeByFilePath(string $filepath): string + protected function getMediaTypeByFilePath(string $filepath): MediaType { $info = @getimagesize($filepath); @@ -29,7 +30,7 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ throw new DecoderException('Unable to decode input'); } - return $info['mime']; + return MediaType::from($info['mime']); } /** @@ -37,9 +38,9 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ * * @param string $data * @throws DecoderException - * @return string + * @return MediaType */ - protected function getMediaTypeByBinary(string $data): string + protected function getMediaTypeByBinary(string $data): MediaType { $info = @getimagesizefromstring($data); @@ -51,6 +52,6 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ throw new DecoderException('Unable to decode input'); } - return $info['mime']; + return MediaType::from($info['mime']); } } diff --git a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php index 3dd71cb7..acba9df9 100644 --- a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Gd\Decoders; use Intervention\Image\Exceptions\DecoderException; +use Intervention\Image\Format; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -21,23 +22,16 @@ class FilePathImageDecoder extends NativeObjectDecoder implements DecoderInterfa // detect media (mime) type $mediaType = $this->getMediaTypeByFilePath($input); - $image = match ($mediaType) { + $image = match ($mediaType->format()) { // 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), - 'image/webp', 'image/x-webp' => @imagecreatefromwebp($input), - 'image/png', 'image/x-png' => @imagecreatefrompng($input), - 'image/avif', 'image/x-avif' => @imagecreatefromavif($input), - 'image/bmp', - 'image/ms-bmp', - 'image/x-bitmap', - 'image/x-bmp', - 'image/x-ms-bmp', - 'image/x-win-bitmap', - 'image/x-windows-bmp', - 'image/x-xbitmap' => @imagecreatefrombmp($input), + Format::GIF => $this->decodeGif($input), + default => parent::decode(match ($mediaType->format()) { + Format::JPEG => @imagecreatefromjpeg($input), + Format::WEBP => @imagecreatefromwebp($input), + Format::PNG => @imagecreatefrompng($input), + Format::AVIF => @imagecreatefromavif($input), + Format::BMP => @imagecreatefrombmp($input), default => throw new DecoderException('Unable to decode input'), }), }; diff --git a/src/Origin.php b/src/Origin.php index 9e5413a5..4cd7a8e4 100644 --- a/src/Origin.php +++ b/src/Origin.php @@ -40,12 +40,15 @@ class Origin /** * Set media type of current instance * - * @param string $type + * @param string|MediaType $type * @return Origin */ - public function setMediaType(string $type): self + public function setMediaType(string|MediaType $type): self { - $this->mediaType = $type; + $this->mediaType = match (true) { + is_string($type) => $type, + default => $type->value, + }; return $this; } diff --git a/tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php index 264c9ed5..03ecf5d4 100644 --- a/tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php +++ b/tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Intervention\Image\Tests\Unit\Drivers\Gd\Decoders; use Intervention\Image\Drivers\Gd\Decoders\AbstractDecoder; +use Intervention\Image\MediaType; use Intervention\Image\Tests\BaseTestCase; use Mockery; @@ -13,12 +14,18 @@ final class AbstractDecoderTest extends BaseTestCase public function testGetMediaTypeFromFilePath(): void { $decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); - $this->assertEquals('image/jpeg', $decoder->getMediaTypeByFilePath($this->getTestResourcePath('test.jpg'))); + $this->assertEquals( + MediaType::IMAGE_JPEG, + $decoder->getMediaTypeByFilePath($this->getTestResourcePath('test.jpg')) + ); } public function testGetMediaTypeFromFileBinary(): void { $decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); - $this->assertEquals('image/jpeg', $decoder->getMediaTypeByBinary($this->getTestResourceData('test.jpg'))); + $this->assertEquals( + MediaType::IMAGE_JPEG, + $decoder->getMediaTypeByBinary($this->getTestResourceData('test.jpg')), + ); } }