diff --git a/src/Decoders/Base64ImageDecoder.php b/src/Decoders/Base64ImageDecoder.php new file mode 100644 index 00000000..f15a90d7 --- /dev/null +++ b/src/Decoders/Base64ImageDecoder.php @@ -0,0 +1,7 @@ +getNamespaceName(); - $class_path = substr(get_class($input), strlen("Intervention\\Image\\")); + $class_path = substr(get_class($object), strlen("Intervention\\Image\\")); $classname = $driver_namespace . "\\" . $class_path; if (!class_exists($classname)) { @@ -44,6 +42,18 @@ abstract class AbstractDriver implements DriverInterface return forward_static_call([ $classname, 'buildSpecialized' - ], $input, $this); + ], $object, $this); + } + + /** + * {@inheritdoc} + * + * @see DriverInterface::specializeMultiple() + */ + public function specializeMultiple(array $objects): array + { + return array_map(function ($classname) { + return $this->specialize(new $classname()); + }, $objects); } } diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index d1598eb7..2db18f48 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -105,7 +105,7 @@ class Driver extends AbstractDriver */ public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface { - return (new InputHandler($decoders))->handle($input); + return (new InputHandler($this->specializeMultiple($decoders)))->handle($input); } /** diff --git a/src/Drivers/Imagick/Driver.php b/src/Drivers/Imagick/Driver.php index aecb11ec..aaf86e81 100644 --- a/src/Drivers/Imagick/Driver.php +++ b/src/Drivers/Imagick/Driver.php @@ -107,7 +107,7 @@ class Driver extends AbstractDriver */ public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface { - return (new InputHandler($decoders))->handle($input); + return (new InputHandler($this->specializeMultiple($decoders)))->handle($input); } /** diff --git a/src/ImageManager.php b/src/ImageManager.php index 1dc61ca7..78ae8fed 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -61,7 +61,7 @@ final class ImageManager } /** - * Create new image instance from given source which can be one of the following + * Create new image instance from given input which can be one of the following * * - Path in filesystem * - File Pointer resource @@ -71,12 +71,28 @@ final class ImageManager * - Data Uri * - Intervention\Image\Image Instance * + * To decode the raw input data, you can optionally specify a decoding strategy + * with the second parameter. This can be an array of class names or objects + * of decoders to be processed in sequence. In this case, the input must be + * decodedable with one of the decoders passed. It is also possible to pass + * a single object or class name of a decoder. + * + * If the second parameter is not set, an attempt to decode the input is made + * with all available decoders of the driver. + * * @param mixed $input + * @param string|array|DecoderInterface $decoders * @return ImageInterface */ - public function read(mixed $input): ImageInterface + public function read(mixed $input, string|array|DecoderInterface $decoders = []): ImageInterface { - return $this->driver->handleInput($input); + return $this->driver->handleInput( + $input, + match (true) { + is_string($decoders), is_a($decoders, DecoderInterface::class) => [$decoders], + default => $decoders, + } + ); } /** diff --git a/src/Interfaces/DriverInterface.php b/src/Interfaces/DriverInterface.php index 057aa429..828d5eae 100644 --- a/src/Interfaces/DriverInterface.php +++ b/src/Interfaces/DriverInterface.php @@ -14,10 +14,18 @@ interface DriverInterface /** * Resolve given object into a specialized version for the current driver * - * @param object $input + * @param object $object * @return ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface */ - public function specialize(object $input): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface; + public function specialize(object $object): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface; + + /** + * Resolve each object in given array into a specialized version for the current driver + * + * @param array $objects + * @return array + */ + public function specializeMultiple(array $objects): array; /** * Create new image instance with the current driver in given dimensions