1
0
mirror of https://github.com/Intervention/image.git synced 2025-07-31 19:10:12 +02:00

Centralize duplicate code

This commit is contained in:
Oliver Vogel
2024-03-08 16:22:59 +01:00
parent 5cd2641a99
commit 2e9f91c311
4 changed files with 36 additions and 28 deletions

View File

@@ -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.

View File

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

View File

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

View File

@@ -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();