1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 21:42:53 +02:00

Implement AnimationFactory::class & AnimationFactoryInterface::class

This commit is contained in:
Oliver Vogel
2025-06-26 16:53:26 +02:00
parent 7153316485
commit 916f0318c9
5 changed files with 109 additions and 78 deletions

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Exceptions\AnimationException;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Interfaces\AnimationFactoryInterface;
use Intervention\Image\Interfaces\CoreInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;
class AnimationFactory implements AnimationFactoryInterface
{
public function __construct(
protected DriverInterface $driver,
public CoreInterface $core = new Core()
) {
//
}
/**
* @throws AnimationException
* @throws DecoderException
*/
public function add(mixed $source, float $delay = 1): self
{
$this->core->add(
$this->driver->handleInput($source)->core()->first()->setDelay($delay)
);
return $this;
}
public function __invoke(): ImageInterface
{
return new Image(
$this->driver,
$this->core
);
}
}

View File

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

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers\Imagick;
use Imagick;
use Intervention\Image\Exceptions\AnimationException;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Interfaces\AnimationFactoryInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;
class AnimationFactory implements AnimationFactoryInterface
{
public function __construct(
protected DriverInterface $driver,
public Imagick $imagick = new Imagick()
) {
$this->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)
);
}
}

View File

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

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Interfaces;
interface AnimationFactoryInterface
{
/**
* Resolve image from given source and add it as new animation frame with specific delay
*/
public function add(mixed $source, float $delay = 1): self;
/**
* Create image instance
*/
public function __invoke(): ImageInterface;
}