1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 05:22:50 +02:00

Implement dedicated reading methods for ImageManager

This commit is contained in:
Oliver Vogel
2025-06-21 15:41:13 +02:00
parent ca0f027679
commit 8956fe6aab
10 changed files with 17 additions and 17 deletions

View File

@@ -143,23 +143,23 @@ abstract class AbstractDecoder implements DecoderInterface
} }
/** /**
* Parse and retunr a given file path or throw detailed exception if the path is invalid * Parse and return a given file path or throw detailed exception if the path is invalid
* *
* @throws DecoderException * @throws DecoderException
*/ */
protected function parseFilePath(mixed $path): string protected function parseFilePath(mixed $path): string
{ {
if (!is_string($path)) { if (!is_string($path)) {
throw new DecoderException('Unable decode image - path must be of type string.'); throw new DecoderException('Path must be of type string.');
} }
if ($path === '') { if ($path === '') {
throw new DecoderException('Unable decode image - path must not be an empty string.'); throw new DecoderException('Path must not be an empty string.');
} }
if (strlen($path) > PHP_MAXPATHLEN) { if (strlen($path) > PHP_MAXPATHLEN) {
throw new DecoderException( throw new DecoderException(
"Unable decode image - the path is longer than the configured max. value of " . PHP_MAXPATHLEN . ".", "Path is longer than the configured max. value of " . PHP_MAXPATHLEN . ".",
); );
} }
@@ -168,11 +168,11 @@ abstract class AbstractDecoder implements DecoderInterface
$basename = pathinfo($path, PATHINFO_BASENAME); $basename = pathinfo($path, PATHINFO_BASENAME);
if (!is_dir($dirname)) { if (!is_dir($dirname)) {
throw new DecoderException("Unable decode image - directory ('" . $dirname . "') does not exist."); throw new DecoderException("Directory ('" . $dirname . "') not found.");
} }
if (!@is_file($path)) { if (!@is_file($path)) {
throw new DecoderException("Unable decode image - file ('" . $basename . "') does not exist."); throw new DecoderException("File ('" . $basename . "') not found.");
} }
return $path; return $path;

View File

@@ -41,7 +41,7 @@ class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface
$gd = @imagecreatefromstring($input); $gd = @imagecreatefromstring($input);
if ($gd === false) { if ($gd === false) {
throw new DecoderException('Unsupported image format.'); throw new DecoderException('Binary data contains unsupported image type.');
} }
// create image instance // create image instance

View File

@@ -19,13 +19,13 @@ class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_string($input)) { if (!is_string($input)) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Data Uri must be of type string.');
} }
$uri = $this->parseDataUri($input); $uri = $this->parseDataUri($input);
if (!$uri->isValid()) { if (!$uri->isValid()) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Input is no valid Data Uri Scheme.');
} }
if ($uri->isBase64Encoded()) { if ($uri->isBase64Encoded()) {

View File

@@ -18,7 +18,7 @@ class FilePointerImageDecoder extends BinaryImageDecoder
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) { if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) {
throw new DecoderException('Unable to decode input'); throw new DecoderException("Input must be a resource of type 'file' or 'stream'.");
} }
$contents = ''; $contents = '';

View File

@@ -20,7 +20,7 @@ class SplFileInfoImageDecoder extends FilePathImageDecoder implements DecoderInt
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_a($input, SplFileInfo::class)) { if (!is_a($input, SplFileInfo::class)) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Input must be of type ' . SplFileInfo::class . '.');
} }
return parent::decode($input->getRealPath()); return parent::decode($input->getRealPath());

View File

@@ -28,7 +28,7 @@ class BinaryImageDecoder extends NativeObjectDecoder
$imagick = new Imagick(); $imagick = new Imagick();
$imagick->readImageBlob($input); $imagick->readImageBlob($input);
} catch (ImagickException) { } catch (ImagickException) {
throw new DecoderException('Unsupported image format.'); throw new DecoderException('Binary data contains unsupported image type.');
} }
// decode image // decode image

View File

@@ -18,13 +18,13 @@ class DataUriImageDecoder extends BinaryImageDecoder
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_string($input)) { if (!is_string($input)) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Data Uri must be of type string.');
} }
$uri = $this->parseDataUri($input); $uri = $this->parseDataUri($input);
if (!$uri->isValid()) { if (!$uri->isValid()) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Input is no valid Data Uri Scheme.');
} }
if ($uri->isBase64Encoded()) { if ($uri->isBase64Encoded()) {

View File

@@ -26,7 +26,7 @@ class FilePathImageDecoder extends NativeObjectDecoder
$imagick = new Imagick(); $imagick = new Imagick();
$imagick->readImage($path); $imagick->readImage($path);
} catch (ImagickException) { } catch (ImagickException) {
throw new DecoderException('Unsupported image format.'); throw new DecoderException('File contains unsupported image format.');
} }
// decode image // decode image

View File

@@ -18,7 +18,7 @@ class FilePointerImageDecoder extends BinaryImageDecoder
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) { if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) {
throw new DecoderException('Unable to decode input'); throw new DecoderException("Input must be a resource of type 'file' or 'stream'.");
} }
$contents = ''; $contents = '';

View File

@@ -19,7 +19,7 @@ class SplFileInfoImageDecoder extends FilePathImageDecoder
public function decode(mixed $input): ImageInterface|ColorInterface public function decode(mixed $input): ImageInterface|ColorInterface
{ {
if (!is_a($input, SplFileInfo::class)) { if (!is_a($input, SplFileInfo::class)) {
throw new DecoderException('Unable to decode input'); throw new DecoderException('Input must be of type ' . SplFileInfo::class . '.');
} }
return parent::decode($input->getRealPath()); return parent::decode($input->getRealPath());