1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 12:18:14 +01:00

Add methods to create new animated images

This commit is contained in:
Oliver Vogel 2023-10-03 10:18:17 +02:00
parent bc69b936c5
commit 8eb1394b99
5 changed files with 103 additions and 0 deletions

View File

@ -3,11 +3,16 @@
namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Collection;
use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Drivers\Gd\Image;
use Intervention\Image\Interfaces\FactoryInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanHandleInput;
class ImageFactory implements FactoryInterface
{
use CanHandleInput;
public function newImage(int $width, int $height): ImageInterface
{
return new Image(
@ -17,6 +22,34 @@ class ImageFactory implements FactoryInterface
);
}
public function newAnimation(callable $callback): ImageInterface
{
$frames = new Collection();
$animation = new class ($frames) extends ImageFactory
{
public function __construct(public Collection $frames)
{
//
}
public function add($source, float $delay = 1): self
{
$this->frames->push(
$this->handleInput($source)
->getFrame()
->setDelay($delay)
);
return $this;
}
};
$callback($animation);
return new Image($frames);
}
public function newCore(int $width, int $height)
{
$core = imagecreatetruecolor($width, $height);

View File

@ -4,16 +4,53 @@ namespace Intervention\Image\Drivers\Imagick;
use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Interfaces\FactoryInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanCheckType;
use Intervention\Image\Traits\CanHandleInput;
class ImageFactory implements FactoryInterface
{
use CanHandleInput;
use CanCheckType;
public function newImage(int $width, int $height): ImageInterface
{
return new Image($this->newCore($width, $height));
}
public function newAnimation(callable $callback): ImageInterface
{
$imagick = new Imagick();
$imagick->setFormat('gif');
$animation = new class ($imagick) extends ImageFactory
{
public function __construct(public Imagick $imagick)
{
//
}
public function add($source, float $delay = 1): self
{
$imagick = $this->failIfNotClass(
$this->handleInput($source),
Image::class,
)->getImagick();
$imagick->setImageDelay($delay * 100);
$this->imagick->addImage($imagick);
return $this;
}
};
$callback($animation);
return new Image($animation->imagick);
}
public function newCore(int $width, int $height)
{
$imagick = new Imagick();

View File

@ -26,6 +26,17 @@ class ImageManager
return $this->resolveDriverClass('ImageFactory')->newImage($width, $height);
}
/**
* Create new animated image from sources
*
* @param callable $callback
* @return ImageInterface
*/
public function animate(callable $callback): ImageInterface
{
return $this->resolveDriverClass('ImageFactory')->newAnimation($callback);
}
/**
* Create new image instance from source
*

View File

@ -20,6 +20,17 @@ class ImageFactoryTest extends TestCase
$this->assertInstanceOf(Image::class, $image);
}
public function testNewAnimation(): void
{
$factory = new ImageFactory();
$image = $factory->newAnimation(function ($animation) {
$animation->add($this->getTestImagePath('blue.gif'), 1.2);
$animation->add($this->getTestImagePath('red.gif'), 1.2);
});
$this->assertInstanceOf(Image::class, $image);
$this->assertEquals(2, $image->count());
}
public function testNewCore(): void
{
$factory = new ImageFactory();

View File

@ -20,6 +20,17 @@ class ImageFactoryTest extends TestCase
$this->assertInstanceOf(Image::class, $image);
}
public function testNewAnimation(): void
{
$factory = new ImageFactory();
$image = $factory->newAnimation(function ($animation) {
$animation->add($this->getTestImagePath('blue.gif'), 1.2);
$animation->add($this->getTestImagePath('red.gif'), 1.2);
});
$this->assertInstanceOf(Image::class, $image);
$this->assertEquals(2, $image->count());
}
public function testNewCore(): void
{
$factory = new ImageFactory();