1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-02 18:32:56 +02:00

Change signature of internal methods

The following internal methods now return enum MediaType instead of
string.

Intervention\Image\AbstractDecoder::getMediaTypeByFilePath()
Intervention\Image\AbstractDecoder::getMediaTypeByBinary()
This commit is contained in:
Oliver Vogel
2024-06-26 17:13:30 +02:00
parent c199536eac
commit bbf0f9f821
4 changed files with 31 additions and 26 deletions

View File

@@ -7,6 +7,7 @@ namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\SpecializableDecoder; use Intervention\Image\Drivers\SpecializableDecoder;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\MediaType;
abstract class AbstractDecoder extends SpecializableDecoder implements SpecializedInterface abstract class AbstractDecoder extends SpecializableDecoder implements SpecializedInterface
{ {
@@ -15,9 +16,9 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ
* *
* @param string $filepath * @param string $filepath
* @throws DecoderException * @throws DecoderException
* @return string * @return MediaType
*/ */
protected function getMediaTypeByFilePath(string $filepath): string protected function getMediaTypeByFilePath(string $filepath): MediaType
{ {
$info = @getimagesize($filepath); $info = @getimagesize($filepath);
@@ -29,7 +30,7 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ
throw new DecoderException('Unable to decode input'); 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 * @param string $data
* @throws DecoderException * @throws DecoderException
* @return string * @return MediaType
*/ */
protected function getMediaTypeByBinary(string $data): string protected function getMediaTypeByBinary(string $data): MediaType
{ {
$info = @getimagesizefromstring($data); $info = @getimagesizefromstring($data);
@@ -51,6 +52,6 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ
throw new DecoderException('Unable to decode input'); throw new DecoderException('Unable to decode input');
} }
return $info['mime']; return MediaType::from($info['mime']);
} }
} }

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Intervention\Image\Drivers\Gd\Decoders; namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Format;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
@@ -21,23 +22,16 @@ class FilePathImageDecoder extends NativeObjectDecoder implements DecoderInterfa
// detect media (mime) type // detect media (mime) type
$mediaType = $this->getMediaTypeByFilePath($input); $mediaType = $this->getMediaTypeByFilePath($input);
$image = match ($mediaType) { $image = match ($mediaType->format()) {
// gif files might be animated and therefore cannot // gif files might be animated and therefore cannot
// be handled by the standard GD decoder. // be handled by the standard GD decoder.
'image/gif' => $this->decodeGif($input), Format::GIF => $this->decodeGif($input),
default => parent::decode(match ($mediaType) { default => parent::decode(match ($mediaType->format()) {
'image/jpeg', 'image/jpg', 'image/pjpeg' => @imagecreatefromjpeg($input), Format::JPEG => @imagecreatefromjpeg($input),
'image/webp', 'image/x-webp' => @imagecreatefromwebp($input), Format::WEBP => @imagecreatefromwebp($input),
'image/png', 'image/x-png' => @imagecreatefrompng($input), Format::PNG => @imagecreatefrompng($input),
'image/avif', 'image/x-avif' => @imagecreatefromavif($input), Format::AVIF => @imagecreatefromavif($input),
'image/bmp', Format::BMP => @imagecreatefrombmp($input),
'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),
default => throw new DecoderException('Unable to decode input'), default => throw new DecoderException('Unable to decode input'),
}), }),
}; };

View File

@@ -40,12 +40,15 @@ class Origin
/** /**
* Set media type of current instance * Set media type of current instance
* *
* @param string $type * @param string|MediaType $type
* @return Origin * @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; return $this;
} }

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Gd\Decoders; namespace Intervention\Image\Tests\Unit\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\AbstractDecoder; use Intervention\Image\Drivers\Gd\Decoders\AbstractDecoder;
use Intervention\Image\MediaType;
use Intervention\Image\Tests\BaseTestCase; use Intervention\Image\Tests\BaseTestCase;
use Mockery; use Mockery;
@@ -13,12 +14,18 @@ final class AbstractDecoderTest extends BaseTestCase
public function testGetMediaTypeFromFilePath(): void public function testGetMediaTypeFromFilePath(): void
{ {
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); $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 public function testGetMediaTypeFromFileBinary(): void
{ {
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); $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')),
);
} }
} }