1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 01:29:51 +02:00

Add generic decoders

This commit is contained in:
Oliver Vogel
2024-01-05 12:34:34 +01:00
parent 0fe3834385
commit b9c6368656
11 changed files with 92 additions and 16 deletions

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class Base64ImageDecoder extends AbstractDecoder
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class BinaryImageDecoder extends AbstractDecoder
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class DataUriImageDecoder extends AbstractDecoder
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class FilePointerImageDecoder extends AbstractDecoder
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class ImageObjectDecoder extends AbstractDecoder
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Decoders;
class SplFileInfoImageDecoder extends AbstractDecoder
{
}

View File

@@ -19,20 +19,18 @@ abstract class AbstractDriver implements DriverInterface
}
/**
* Return a specialized version for the current driver of the given object
* {@inheritdoc}
*
* @param object $input
* @return ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
* @throws NotSupportedException
* @see DriverInterface::specialize()
*/
public function specialize(object $input): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
public function specialize(object $object): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
{
if (!($input instanceof SpecializableInterface)) {
return $input;
if (!($object instanceof SpecializableInterface)) {
return $object;
}
$driver_namespace = (new ReflectionClass($this))->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);
}
}

View File

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

View File

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

View File

@@ -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,
}
);
}
/**

View File

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