mirror of
https://github.com/Intervention/image.git
synced 2025-08-10 16:04:04 +02:00
Centralize duplicate code
This commit is contained in:
@@ -68,6 +68,33 @@ abstract class AbstractDecoder extends DriverSpecialized implements DecoderInter
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if given input is a path to an existing regular file
|
||||||
|
*
|
||||||
|
* @param mixed $input
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isFile(mixed $input): bool
|
||||||
|
{
|
||||||
|
if (!is_string($input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen($input) > PHP_MAXPATHLEN) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!@is_file($input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (Exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract and return EXIF data from given input which can be binary image
|
* Extract and return EXIF data from given input which can be binary image
|
||||||
* data or a file path.
|
* data or a file path.
|
||||||
|
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Gd\Decoders;
|
namespace Intervention\Image\Drivers\Gd\Decoders;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\Traits\CanDecodeGif;
|
use Intervention\Image\Drivers\Gd\Decoders\Traits\CanDecodeGif;
|
||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
@@ -18,19 +17,7 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface
|
|||||||
|
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
if (!is_string($input)) {
|
if (!$this->isFile($input)) {
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($input) > PHP_MAXPATHLEN) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!@is_file($input)) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
} catch (Exception) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
throw new DecoderException('Unable to decode input');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Imagick\Decoders;
|
namespace Intervention\Image\Drivers\Imagick\Decoders;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Imagick;
|
use Imagick;
|
||||||
use ImagickException;
|
use ImagickException;
|
||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
@@ -16,19 +15,7 @@ class FilePathImageDecoder extends ImagickImageDecoder implements DecoderInterfa
|
|||||||
{
|
{
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
if (!is_string($input)) {
|
if (!$this->isFile($input)) {
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($input) > PHP_MAXPATHLEN) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!@is_file($input)) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
|
||||||
}
|
|
||||||
} catch (Exception) {
|
|
||||||
throw new DecoderException('Unable to decode input');
|
throw new DecoderException('Unable to decode input');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -58,6 +58,13 @@ final class AbstractDecoderTest extends BaseTestCase
|
|||||||
$this->assertTrue($decoder->isGifFormat($this->getTestImageData('red.gif')));
|
$this->assertTrue($decoder->isGifFormat($this->getTestImageData('red.gif')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIsFile(): void
|
||||||
|
{
|
||||||
|
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
|
||||||
|
$this->assertTrue($decoder->isFile($this->getTestImagePath()));
|
||||||
|
$this->assertFalse($decoder->isFile('non-existent-file'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testExtractExifDataFromBinary(): void
|
public function testExtractExifDataFromBinary(): void
|
||||||
{
|
{
|
||||||
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
|
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
|
||||||
|
Reference in New Issue
Block a user