mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 17:19:50 +02:00
Add ability to read Imagick or GDImage directly (#1329)
This commit is contained in:
11
src/Decoders/NativeObjectDecoder.php
Normal file
11
src/Decoders/NativeObjectDecoder.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Intervention\Image\Decoders;
|
||||||
|
|
||||||
|
use Intervention\Image\Drivers\SpecializableDecoder;
|
||||||
|
|
||||||
|
class NativeObjectDecoder extends SpecializableDecoder
|
||||||
|
{
|
||||||
|
}
|
@@ -12,7 +12,7 @@ use Intervention\Image\Drivers\Gd\Decoders\Traits\CanDecodeGif;
|
|||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
use Intervention\Image\Modifiers\AlignRotationModifier;
|
use Intervention\Image\Modifiers\AlignRotationModifier;
|
||||||
|
|
||||||
class BinaryImageDecoder extends GdImageDecoder implements DecoderInterface
|
class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface
|
||||||
{
|
{
|
||||||
use CanDecodeGif;
|
use CanDecodeGif;
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@ use Intervention\Image\Interfaces\DecoderInterface;
|
|||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Modifiers\AlignRotationModifier;
|
use Intervention\Image\Modifiers\AlignRotationModifier;
|
||||||
|
|
||||||
class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface
|
class FilePathImageDecoder extends NativeObjectDecoder implements DecoderInterface
|
||||||
{
|
{
|
||||||
use CanDecodeGif;
|
use CanDecodeGif;
|
||||||
|
|
||||||
|
@@ -13,8 +13,13 @@ use Intervention\Image\Image;
|
|||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
|
|
||||||
class GdImageDecoder extends AbstractDecoder
|
class NativeObjectDecoder extends AbstractDecoder
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see DecoderInterface::decode()
|
||||||
|
*/
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
if (!is_object($input)) {
|
if (!is_object($input)) {
|
@@ -19,11 +19,13 @@ use Intervention\Image\Drivers\Gd\Decoders\FilePathImageDecoder;
|
|||||||
use Intervention\Image\Drivers\Gd\Decoders\BinaryImageDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\BinaryImageDecoder;
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\DataUriImageDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\DataUriImageDecoder;
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\Base64ImageDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\Base64ImageDecoder;
|
||||||
|
use Intervention\Image\Drivers\Gd\Decoders\NativeObjectDecoder;
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\SplFileInfoImageDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\SplFileInfoImageDecoder;
|
||||||
|
|
||||||
class InputHandler extends AbstractInputHandler
|
class InputHandler extends AbstractInputHandler
|
||||||
{
|
{
|
||||||
protected array $decoders = [
|
protected array $decoders = [
|
||||||
|
NativeObjectDecoder::class,
|
||||||
ImageObjectDecoder::class,
|
ImageObjectDecoder::class,
|
||||||
ColorObjectDecoder::class,
|
ColorObjectDecoder::class,
|
||||||
RgbHexColorDecoder::class,
|
RgbHexColorDecoder::class,
|
||||||
|
@@ -10,7 +10,7 @@ use Intervention\Image\Exceptions\DecoderException;
|
|||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
|
||||||
class BinaryImageDecoder extends ImagickImageDecoder
|
class BinaryImageDecoder extends NativeObjectDecoder
|
||||||
{
|
{
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
|
@@ -10,7 +10,7 @@ use Intervention\Image\Exceptions\DecoderException;
|
|||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
|
||||||
class FilePathImageDecoder extends ImagickImageDecoder
|
class FilePathImageDecoder extends NativeObjectDecoder
|
||||||
{
|
{
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
|
@@ -14,7 +14,7 @@ use Intervention\Image\Interfaces\ColorInterface;
|
|||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Modifiers\AlignRotationModifier;
|
use Intervention\Image\Modifiers\AlignRotationModifier;
|
||||||
|
|
||||||
class ImagickImageDecoder extends SpecializableDecoder
|
class NativeObjectDecoder extends SpecializableDecoder
|
||||||
{
|
{
|
||||||
public function decode(mixed $input): ImageInterface|ColorInterface
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
@@ -19,11 +19,13 @@ use Intervention\Image\Drivers\Imagick\Decoders\FilePathImageDecoder;
|
|||||||
use Intervention\Image\Drivers\Imagick\Decoders\BinaryImageDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\BinaryImageDecoder;
|
||||||
use Intervention\Image\Drivers\Imagick\Decoders\DataUriImageDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\DataUriImageDecoder;
|
||||||
use Intervention\Image\Drivers\Imagick\Decoders\Base64ImageDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\Base64ImageDecoder;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Decoders\NativeObjectDecoder;
|
||||||
use Intervention\Image\Drivers\Imagick\Decoders\SplFileInfoImageDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\SplFileInfoImageDecoder;
|
||||||
|
|
||||||
class InputHandler extends AbstractInputHandler
|
class InputHandler extends AbstractInputHandler
|
||||||
{
|
{
|
||||||
protected array $decoders = [
|
protected array $decoders = [
|
||||||
|
NativeObjectDecoder::class,
|
||||||
ImageObjectDecoder::class,
|
ImageObjectDecoder::class,
|
||||||
ColorObjectDecoder::class,
|
ColorObjectDecoder::class,
|
||||||
RgbHexColorDecoder::class,
|
RgbHexColorDecoder::class,
|
||||||
|
@@ -34,6 +34,13 @@ final class InputHandlerTest extends BaseTestCase
|
|||||||
$this->assertInstanceOf(Image::class, $result);
|
$this->assertInstanceOf(Image::class, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testHandleGdImage(): void
|
||||||
|
{
|
||||||
|
$handler = new InputHandler();
|
||||||
|
$result = $handler->handle(imagecreatetruecolor(3, 2));
|
||||||
|
$this->assertInstanceOf(Image::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
public function testHandleSplFileInfo(): void
|
public function testHandleSplFileInfo(): void
|
||||||
{
|
{
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
|
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick;
|
namespace Intervention\Image\Tests\Unit\Drivers\Imagick;
|
||||||
|
|
||||||
|
use Imagick;
|
||||||
|
use ImagickPixel;
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
@@ -34,6 +36,15 @@ final class InputHandlerTest extends BaseTestCase
|
|||||||
$this->assertInstanceOf(Image::class, $result);
|
$this->assertInstanceOf(Image::class, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testHandleImagick(): void
|
||||||
|
{
|
||||||
|
$imagick = new Imagick();
|
||||||
|
$imagick->newImage(3, 2, new ImagickPixel('rgba(255, 255, 255, 255)'), 'png');
|
||||||
|
$handler = new InputHandler();
|
||||||
|
$result = $handler->handle($imagick);
|
||||||
|
$this->assertInstanceOf(Image::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
public function testHandleSplFileInfo(): void
|
public function testHandleSplFileInfo(): void
|
||||||
{
|
{
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
|
Reference in New Issue
Block a user