Skip to content

Commit

Permalink
Add methods to create new animated images
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 3, 2023
1 parent bc69b93 commit 8eb1394
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Drivers/Gd/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -17,6 +22,34 @@ public function newImage(int $width, int $height): ImageInterface
);
}

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);
Expand Down
37 changes: 37 additions & 0 deletions src/Drivers/Imagick/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,53 @@

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();
Expand Down
11 changes: 11 additions & 0 deletions src/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public function create(int $width, int $height): ImageInterface
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
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Gd/ImageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ public function testNewImage(): void
$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();
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Imagick/ImageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ public function testNewImage(): void
$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();
Expand Down

0 comments on commit 8eb1394

Please sign in to comment.