mirror of
https://github.com/Intervention/image.git
synced 2025-08-11 16:34:00 +02:00
Add methods to create new animated images
This commit is contained in:
@@ -3,11 +3,16 @@
|
|||||||
namespace Intervention\Image\Drivers\Gd;
|
namespace Intervention\Image\Drivers\Gd;
|
||||||
|
|
||||||
use Intervention\Image\Collection;
|
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\FactoryInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Traits\CanHandleInput;
|
||||||
|
|
||||||
class ImageFactory implements FactoryInterface
|
class ImageFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
|
use CanHandleInput;
|
||||||
|
|
||||||
public function newImage(int $width, int $height): ImageInterface
|
public function newImage(int $width, int $height): ImageInterface
|
||||||
{
|
{
|
||||||
return new Image(
|
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)
|
public function newCore(int $width, int $height)
|
||||||
{
|
{
|
||||||
$core = imagecreatetruecolor($width, $height);
|
$core = imagecreatetruecolor($width, $height);
|
||||||
|
@@ -4,16 +4,53 @@ namespace Intervention\Image\Drivers\Imagick;
|
|||||||
|
|
||||||
use Imagick;
|
use Imagick;
|
||||||
use ImagickPixel;
|
use ImagickPixel;
|
||||||
|
use Intervention\Image\Drivers\Imagick\Image;
|
||||||
use Intervention\Image\Interfaces\FactoryInterface;
|
use Intervention\Image\Interfaces\FactoryInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Traits\CanCheckType;
|
||||||
|
use Intervention\Image\Traits\CanHandleInput;
|
||||||
|
|
||||||
class ImageFactory implements FactoryInterface
|
class ImageFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
|
use CanHandleInput;
|
||||||
|
use CanCheckType;
|
||||||
|
|
||||||
public function newImage(int $width, int $height): ImageInterface
|
public function newImage(int $width, int $height): ImageInterface
|
||||||
{
|
{
|
||||||
return new Image($this->newCore($width, $height));
|
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)
|
public function newCore(int $width, int $height)
|
||||||
{
|
{
|
||||||
$imagick = new Imagick();
|
$imagick = new Imagick();
|
||||||
|
@@ -26,6 +26,17 @@ class ImageManager
|
|||||||
return $this->resolveDriverClass('ImageFactory')->newImage($width, $height);
|
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
|
* Create new image instance from source
|
||||||
*
|
*
|
||||||
|
@@ -20,6 +20,17 @@ class ImageFactoryTest extends TestCase
|
|||||||
$this->assertInstanceOf(Image::class, $image);
|
$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
|
public function testNewCore(): void
|
||||||
{
|
{
|
||||||
$factory = new ImageFactory();
|
$factory = new ImageFactory();
|
||||||
|
@@ -20,6 +20,17 @@ class ImageFactoryTest extends TestCase
|
|||||||
$this->assertInstanceOf(Image::class, $image);
|
$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
|
public function testNewCore(): void
|
||||||
{
|
{
|
||||||
$factory = new ImageFactory();
|
$factory = new ImageFactory();
|
||||||
|
Reference in New Issue
Block a user