From 916f0318c9b81920ae4fcac0a7a70e804b6a7013 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 26 Jun 2025 16:53:26 +0200 Subject: [PATCH] Implement AnimationFactory::class & AnimationFactoryInterface::class --- src/Drivers/Gd/AnimationFactory.php | 44 +++++++++++++++++++ src/Drivers/Gd/Driver.php | 38 +---------------- src/Drivers/Imagick/AnimationFactory.php | 45 ++++++++++++++++++++ src/Drivers/Imagick/Driver.php | 42 +----------------- src/Interfaces/AnimationFactoryInterface.php | 18 ++++++++ 5 files changed, 109 insertions(+), 78 deletions(-) create mode 100644 src/Drivers/Gd/AnimationFactory.php create mode 100644 src/Drivers/Imagick/AnimationFactory.php create mode 100644 src/Interfaces/AnimationFactoryInterface.php diff --git a/src/Drivers/Gd/AnimationFactory.php b/src/Drivers/Gd/AnimationFactory.php new file mode 100644 index 00000000..b3e38ce6 --- /dev/null +++ b/src/Drivers/Gd/AnimationFactory.php @@ -0,0 +1,44 @@ +core->add( + $this->driver->handleInput($source)->core()->first()->setDelay($delay) + ); + + return $this; + } + + public function __invoke(): ImageInterface + { + return new Image( + $this->driver, + $this->core + ); + } +} diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index 2110b41d..6d20ef8a 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -6,13 +6,11 @@ namespace Intervention\Image\Drivers\Gd; use Intervention\Image\Drivers\AbstractDriver; use Intervention\Image\Exceptions\DriverException; -use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Format; use Intervention\Image\FileExtension; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorProcessorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -use Intervention\Image\Interfaces\DriverInterface; use Intervention\Image\Interfaces\FontProcessorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\MediaType; @@ -72,44 +70,10 @@ class Driver extends AbstractDriver * {@inheritdoc} * * @see DriverInterface::createAnimation() - * - * @throws RuntimeException */ public function createAnimation(callable $init): ImageInterface { - $animation = new class ($this) - { - public function __construct( - protected DriverInterface $driver, - public Core $core = new Core() - ) { - // - } - - /** - * @throws RuntimeException - */ - public function add(mixed $source, float $delay = 1): self - { - $this->core->add( - $this->driver->handleInput($source)->core()->first()->setDelay($delay) - ); - - return $this; - } - - /** - * @throws RuntimeException - */ - public function __invoke(): ImageInterface - { - return new Image( - $this->driver, - $this->core - ); - } - }; - + $animation = new AnimationFactory($this); $init($animation); return call_user_func($animation); diff --git a/src/Drivers/Imagick/AnimationFactory.php b/src/Drivers/Imagick/AnimationFactory.php new file mode 100644 index 00000000..65f92ead --- /dev/null +++ b/src/Drivers/Imagick/AnimationFactory.php @@ -0,0 +1,45 @@ +imagick->setFormat('gif'); + } + + /** + * @throws AnimationException + * @throws DecoderException + */ + public function add(mixed $source, float $delay = 1): self + { + $native = $this->driver->handleInput($source)->core()->native(); + $native->setImageDelay(intval(round($delay * 100))); + + $this->imagick->addImage($native); + + return $this; + } + + public function __invoke(): ImageInterface + { + return new Image( + $this->driver, + new Core($this->imagick) + ); + } +} diff --git a/src/Drivers/Imagick/Driver.php b/src/Drivers/Imagick/Driver.php index 7c1cb632..d6b86936 100644 --- a/src/Drivers/Imagick/Driver.php +++ b/src/Drivers/Imagick/Driver.php @@ -9,13 +9,11 @@ use ImagickPixel; use Intervention\Image\Drivers\AbstractDriver; use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\NotSupportedException; -use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Format; use Intervention\Image\FileExtension; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorProcessorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -use Intervention\Image\Interfaces\DriverInterface; use Intervention\Image\Interfaces\FontProcessorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\MediaType; @@ -72,48 +70,10 @@ class Driver extends AbstractDriver * {@inheritdoc} * * @see DriverInterface::createAnimation() - * - * @throws RuntimeException */ public function createAnimation(callable $init): ImageInterface { - $imagick = new Imagick(); - $imagick->setFormat('gif'); - - $animation = new class ($this, $imagick) - { - public function __construct( - protected DriverInterface $driver, - public Imagick $imagick - ) { - // - } - - /** - * @throws RuntimeException - */ - public function add(mixed $source, float $delay = 1): self - { - $native = $this->driver->handleInput($source)->core()->native(); - $native->setImageDelay(intval(round($delay * 100))); - - $this->imagick->addImage($native); - - return $this; - } - - /** - * @throws RuntimeException - */ - public function __invoke(): ImageInterface - { - return new Image( - $this->driver, - new Core($this->imagick) - ); - } - }; - + $animation = new AnimationFactory($this); $init($animation); return call_user_func($animation); diff --git a/src/Interfaces/AnimationFactoryInterface.php b/src/Interfaces/AnimationFactoryInterface.php new file mode 100644 index 00000000..25d8920f --- /dev/null +++ b/src/Interfaces/AnimationFactoryInterface.php @@ -0,0 +1,18 @@ +