mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 17:19:50 +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:
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
@@ -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'),
|
||||
}),
|
||||
};
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user