Skip to content

Commit bbf0f9f

Browse files
committed
Change signature of internal methods
The following internal methods now return enum MediaType instead of string. Intervention\Image\AbstractDecoder::getMediaTypeByFilePath() Intervention\Image\AbstractDecoder::getMediaTypeByBinary()
1 parent c199536 commit bbf0f9f

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

src/Drivers/Gd/Decoders/AbstractDecoder.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Intervention\Image\Drivers\SpecializableDecoder;
88
use Intervention\Image\Exceptions\DecoderException;
99
use Intervention\Image\Interfaces\SpecializedInterface;
10+
use Intervention\Image\MediaType;
1011

1112
abstract class AbstractDecoder extends SpecializableDecoder implements SpecializedInterface
1213
{
@@ -15,9 +16,9 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ
1516
*
1617
* @param string $filepath
1718
* @throws DecoderException
18-
* @return string
19+
* @return MediaType
1920
*/
20-
protected function getMediaTypeByFilePath(string $filepath): string
21+
protected function getMediaTypeByFilePath(string $filepath): MediaType
2122
{
2223
$info = @getimagesize($filepath);
2324

@@ -29,17 +30,17 @@ protected function getMediaTypeByFilePath(string $filepath): string
2930
throw new DecoderException('Unable to decode input');
3031
}
3132

32-
return $info['mime'];
33+
return MediaType::from($info['mime']);
3334
}
3435

3536
/**
3637
* Return media (mime) type of the given image data
3738
*
3839
* @param string $data
3940
* @throws DecoderException
40-
* @return string
41+
* @return MediaType
4142
*/
42-
protected function getMediaTypeByBinary(string $data): string
43+
protected function getMediaTypeByBinary(string $data): MediaType
4344
{
4445
$info = @getimagesizefromstring($data);
4546

@@ -51,6 +52,6 @@ protected function getMediaTypeByBinary(string $data): string
5152
throw new DecoderException('Unable to decode input');
5253
}
5354

54-
return $info['mime'];
55+
return MediaType::from($info['mime']);
5556
}
5657
}

src/Drivers/Gd/Decoders/FilePathImageDecoder.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Intervention\Image\Drivers\Gd\Decoders;
66

77
use Intervention\Image\Exceptions\DecoderException;
8+
use Intervention\Image\Format;
89
use Intervention\Image\Interfaces\ColorInterface;
910
use Intervention\Image\Interfaces\DecoderInterface;
1011
use Intervention\Image\Interfaces\ImageInterface;
@@ -21,23 +22,16 @@ public function decode(mixed $input): ImageInterface|ColorInterface
2122
// detect media (mime) type
2223
$mediaType = $this->getMediaTypeByFilePath($input);
2324

24-
$image = match ($mediaType) {
25+
$image = match ($mediaType->format()) {
2526
// gif files might be animated and therefore cannot
2627
// be handled by the standard GD decoder.
27-
'image/gif' => $this->decodeGif($input),
28-
default => parent::decode(match ($mediaType) {
29-
'image/jpeg', 'image/jpg', 'image/pjpeg' => @imagecreatefromjpeg($input),
30-
'image/webp', 'image/x-webp' => @imagecreatefromwebp($input),
31-
'image/png', 'image/x-png' => @imagecreatefrompng($input),
32-
'image/avif', 'image/x-avif' => @imagecreatefromavif($input),
33-
'image/bmp',
34-
'image/ms-bmp',
35-
'image/x-bitmap',
36-
'image/x-bmp',
37-
'image/x-ms-bmp',
38-
'image/x-win-bitmap',
39-
'image/x-windows-bmp',
40-
'image/x-xbitmap' => @imagecreatefrombmp($input),
28+
Format::GIF => $this->decodeGif($input),
29+
default => parent::decode(match ($mediaType->format()) {
30+
Format::JPEG => @imagecreatefromjpeg($input),
31+
Format::WEBP => @imagecreatefromwebp($input),
32+
Format::PNG => @imagecreatefrompng($input),
33+
Format::AVIF => @imagecreatefromavif($input),
34+
Format::BMP => @imagecreatefrombmp($input),
4135
default => throw new DecoderException('Unable to decode input'),
4236
}),
4337
};

src/Origin.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ public function mimetype(): string
4040
/**
4141
* Set media type of current instance
4242
*
43-
* @param string $type
43+
* @param string|MediaType $type
4444
* @return Origin
4545
*/
46-
public function setMediaType(string $type): self
46+
public function setMediaType(string|MediaType $type): self
4747
{
48-
$this->mediaType = $type;
48+
$this->mediaType = match (true) {
49+
is_string($type) => $type,
50+
default => $type->value,
51+
};
4952

5053
return $this;
5154
}

tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Intervention\Image\Tests\Unit\Drivers\Gd\Decoders;
66

77
use Intervention\Image\Drivers\Gd\Decoders\AbstractDecoder;
8+
use Intervention\Image\MediaType;
89
use Intervention\Image\Tests\BaseTestCase;
910
use Mockery;
1011

@@ -13,12 +14,18 @@ final class AbstractDecoderTest extends BaseTestCase
1314
public function testGetMediaTypeFromFilePath(): void
1415
{
1516
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
16-
$this->assertEquals('image/jpeg', $decoder->getMediaTypeByFilePath($this->getTestResourcePath('test.jpg')));
17+
$this->assertEquals(
18+
MediaType::IMAGE_JPEG,
19+
$decoder->getMediaTypeByFilePath($this->getTestResourcePath('test.jpg'))
20+
);
1721
}
1822

1923
public function testGetMediaTypeFromFileBinary(): void
2024
{
2125
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
22-
$this->assertEquals('image/jpeg', $decoder->getMediaTypeByBinary($this->getTestResourceData('test.jpg')));
26+
$this->assertEquals(
27+
MediaType::IMAGE_JPEG,
28+
$decoder->getMediaTypeByBinary($this->getTestResourceData('test.jpg')),
29+
);
2330
}
2431
}

0 commit comments

Comments
 (0)