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:
7
src/Decoders/Base64ImageDecoder.php
Normal file
7
src/Decoders/Base64ImageDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class Base64ImageDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
7
src/Decoders/BinaryImageDecoder.php
Normal file
7
src/Decoders/BinaryImageDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class BinaryImageDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
7
src/Decoders/DataUriImageDecoder.php
Normal file
7
src/Decoders/DataUriImageDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class DataUriImageDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
7
src/Decoders/FilePointerImageDecoder.php
Normal file
7
src/Decoders/FilePointerImageDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class FilePointerImageDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
7
src/Decoders/ImageObjectDecoder.php
Normal file
7
src/Decoders/ImageObjectDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class ImageObjectDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
7
src/Decoders/SplFileInfoImageDecoder.php
Normal file
7
src/Decoders/SplFileInfoImageDecoder.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Decoders;
|
||||
|
||||
class SplFileInfoImageDecoder extends AbstractDecoder
|
||||
{
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user