Skip to content

Commit

Permalink
Change signature of internal methods
Browse files Browse the repository at this point in the history
The following internal methods now return enum MediaType instead of
string.

Intervention\Image\AbstractDecoder::getMediaTypeByFilePath()
Intervention\Image\AbstractDecoder::getMediaTypeByBinary()
  • Loading branch information
olivervogel committed Jun 26, 2024
1 parent c199536 commit bbf0f9f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
13 changes: 7 additions & 6 deletions src/Drivers/Gd/Decoders/AbstractDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
{
Expand All @@ -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);

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

return $info['mime'];
return MediaType::from($info['mime']);
}

/**
* Return media (mime) type of the given image data
*
* @param string $data
* @throws DecoderException
* @return string
* @return MediaType
*/
protected function getMediaTypeByBinary(string $data): string
protected function getMediaTypeByBinary(string $data): MediaType
{
$info = @getimagesizefromstring($data);

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

return $info['mime'];
return MediaType::from($info['mime']);
}
}
24 changes: 9 additions & 15 deletions src/Drivers/Gd/Decoders/FilePathImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -21,23 +22,16 @@ public function decode(mixed $input): ImageInterface|ColorInterface
// 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'),
}),
};
Expand Down
9 changes: 6 additions & 3 deletions src/Origin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ public function mimetype(): string
/**
* 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;
}
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/Drivers/Gd/Decoders/AbstractDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand All @@ -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')),
);
}
}

0 comments on commit bbf0f9f

Please sign in to comment.