mirror of
https://github.com/Intervention/image.git
synced 2025-08-22 21:42:53 +02:00
Move media type detection methods to abstract class
This commit is contained in:
53
src/Drivers/Gd/Decoders/AbstractDecoder.php
Normal file
53
src/Drivers/Gd/Decoders/AbstractDecoder.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Drivers\Gd\Decoders;
|
||||
|
||||
use Intervention\Image\Drivers\AbstractDecoder as GenericAbstractDecoder;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
|
||||
abstract class AbstractDecoder extends GenericAbstractDecoder
|
||||
{
|
||||
/**
|
||||
* Return media (mime) type of the file at given file path
|
||||
*
|
||||
* @param string $filepath
|
||||
* @return string
|
||||
*/
|
||||
protected function getMediaTypeByFilePath(string $filepath): string
|
||||
{
|
||||
$info = getimagesize($filepath);
|
||||
|
||||
if (!is_array($info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
if (!array_key_exists('mime', $info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
return $info['mime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return media (mime) type of the given image data
|
||||
*
|
||||
* @param string $filepath
|
||||
* @return string
|
||||
*/
|
||||
protected function getMediaTypeByBinary(string $data): string
|
||||
{
|
||||
$info = getimagesizefromstring($data);
|
||||
|
||||
if (!is_array($info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
if (!array_key_exists('mime', $info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
return $info['mime'];
|
||||
}
|
||||
}
|
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Drivers\Gd\Decoders;
|
||||
|
||||
use Intervention\Image\Drivers\AbstractDecoder;
|
||||
use Intervention\Image\Drivers\Gd\Frame;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\DecoderInterface;
|
||||
@@ -68,9 +67,12 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
||||
$this->extractExifData($input)
|
||||
);
|
||||
|
||||
// set mediaType on origin
|
||||
if ($info = getimagesizefromstring($input)) {
|
||||
$image->origin()->setMediaType($info['mime']);
|
||||
try {
|
||||
// set mediaType on origin
|
||||
$image->origin()->setMediaType(
|
||||
$this->getMediaTypeByBinary($input)
|
||||
);
|
||||
} catch (DecoderException) {
|
||||
}
|
||||
|
||||
// adjust image orientation
|
||||
|
@@ -37,8 +37,9 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface
|
||||
// detect media (mime) type
|
||||
$mediaType = $this->getMediaTypeByFilePath($input);
|
||||
|
||||
// gif files might be animated and therefore cannot be handled by the standard GD decoder.
|
||||
$image = match ($mediaType) {
|
||||
// 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),
|
||||
@@ -69,25 +70,4 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return media (mime) type of the file at given file path
|
||||
*
|
||||
* @param string $filepath
|
||||
* @return string
|
||||
*/
|
||||
private function getMediaTypeByFilePath(string $filepath): string
|
||||
{
|
||||
$info = getimagesize($filepath);
|
||||
|
||||
if (!is_array($info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
if (!array_key_exists('mime', $info)) {
|
||||
throw new DecoderException('Unable to decode input');
|
||||
}
|
||||
|
||||
return $info['mime'];
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace Intervention\Image\Drivers\Gd\Decoders;
|
||||
|
||||
use GdImage;
|
||||
use Intervention\Image\Drivers\AbstractDecoder;
|
||||
use Intervention\Image\Drivers\Gd\Core;
|
||||
use Intervention\Image\Drivers\Gd\Driver;
|
||||
use Intervention\Image\Drivers\Gd\Frame;
|
||||
|
24
tests/Drivers/Gd/Decoders/AbstractDecoderTest.php
Normal file
24
tests/Drivers/Gd/Decoders/AbstractDecoderTest.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
|
||||
|
||||
use Intervention\Image\Drivers\Gd\Decoders\AbstractDecoder;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class AbstractDecoderTest extends TestCase
|
||||
{
|
||||
public function testGetMediaTypeFromFilePath(): void
|
||||
{
|
||||
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
|
||||
$this->assertEquals('image/jpeg', $decoder->getMediaTypeByFilePath($this->getTestImagePath('test.jpg')));
|
||||
}
|
||||
|
||||
public function testGetMediaTypeFromFileBinary(): void
|
||||
{
|
||||
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
|
||||
$this->assertEquals('image/jpeg', $decoder->getMediaTypeByBinary($this->getTestImageData('test.jpg')));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user