diff --git a/src/Drivers/AbstractDecoder.php b/src/Drivers/AbstractDecoder.php index b376849f..5ada419f 100644 --- a/src/Drivers/AbstractDecoder.php +++ b/src/Drivers/AbstractDecoder.php @@ -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 * data or a file path. diff --git a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php index c88d636e..3cd69185 100644 --- a/src/Drivers/Gd/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Gd/Decoders/FilePathImageDecoder.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Gd\Decoders; -use Exception; use Intervention\Image\Drivers\Gd\Decoders\Traits\CanDecodeGif; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; @@ -18,19 +17,7 @@ class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface public function decode(mixed $input): ImageInterface|ColorInterface { - if (!is_string($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) { + if (!$this->isFile($input)) { throw new DecoderException('Unable to decode input'); } diff --git a/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php b/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php index 7d3d2274..a7e043ce 100644 --- a/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php +++ b/src/Drivers/Imagick/Decoders/FilePathImageDecoder.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Intervention\Image\Drivers\Imagick\Decoders; -use Exception; use Imagick; use ImagickException; use Intervention\Image\Exceptions\DecoderException; @@ -16,19 +15,7 @@ class FilePathImageDecoder extends ImagickImageDecoder implements DecoderInterfa { public function decode(mixed $input): ImageInterface|ColorInterface { - if (!is_string($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) { + if (!$this->isFile($input)) { throw new DecoderException('Unable to decode input'); } diff --git a/tests/Unit/Drivers/AbstractDecoderTest.php b/tests/Unit/Drivers/AbstractDecoderTest.php index ef2ee2c5..890cb00d 100644 --- a/tests/Unit/Drivers/AbstractDecoderTest.php +++ b/tests/Unit/Drivers/AbstractDecoderTest.php @@ -58,6 +58,13 @@ final class AbstractDecoderTest extends BaseTestCase $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 { $decoder = Mockery::mock(AbstractDecoder::class)->makePartial();