From 21acb14db5c71311521814baa4f55ebfb9da0ad6 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 26 Nov 2023 14:21:35 +0100 Subject: [PATCH] Add methods add() to cores --- src/Drivers/Gd/Core.php | 7 +++++++ src/Drivers/Imagick/Core.php | 20 ++++++++++++++++++++ src/Interfaces/CoreInterface.php | 8 ++++++++ tests/Drivers/Gd/CoreTest.php | 11 +++++++++++ tests/Drivers/Imagick/CoreTest.php | 11 +++++++++++ 5 files changed, 57 insertions(+) diff --git a/src/Drivers/Gd/Core.php b/src/Drivers/Gd/Core.php index 210d9ec7..97b1b9fa 100644 --- a/src/Drivers/Gd/Core.php +++ b/src/Drivers/Gd/Core.php @@ -10,6 +10,13 @@ class Core extends Collection implements CoreInterface { protected int $loops = 0; + public function add(FrameInterface $frame): CoreInterface + { + $this->push($frame); + + return $this; + } + public function native(): mixed { return $this->first()->native(); diff --git a/src/Drivers/Imagick/Core.php b/src/Drivers/Imagick/Core.php index 7c4652a2..cf068e9d 100644 --- a/src/Drivers/Imagick/Core.php +++ b/src/Drivers/Imagick/Core.php @@ -17,6 +17,26 @@ public function __construct(protected Imagick $imagick) { } + public function add(FrameInterface $frame): CoreInterface + { + $imagick = $frame->native(); + + $imagick->setImageDelay($frame->delay()); + $imagick->setImageDispose($frame->dispose()); + + $size = $frame->size(); + $imagick->setImagePage( + $size->width(), + $size->height(), + $frame->offsetLeft(), + $frame->offsetTop() + ); + + $this->imagick->addImage($imagick); + + return $this; + } + public function count(): int { return $this->imagick->getNumberImages(); diff --git a/src/Interfaces/CoreInterface.php b/src/Interfaces/CoreInterface.php index 459a4324..16f78382 100644 --- a/src/Interfaces/CoreInterface.php +++ b/src/Interfaces/CoreInterface.php @@ -36,6 +36,14 @@ public function count(): int; */ public function frame(int $position): FrameInterface; + /** + * Add new frame to core + * + * @param FrameInterface $frame + * @return CoreInterface + */ + public function add(FrameInterface $frame): CoreInterface; + /** * Return number of repetitions of an animated image * diff --git a/tests/Drivers/Gd/CoreTest.php b/tests/Drivers/Gd/CoreTest.php index 25f07842..8afc5e5f 100644 --- a/tests/Drivers/Gd/CoreTest.php +++ b/tests/Drivers/Gd/CoreTest.php @@ -17,6 +17,17 @@ public function testNative(): void $this->assertInstanceOf(GdImage::class, $core->native()); } + public function testAdd(): void + { + $gd1 = imagecreatetruecolor(3, 2); + $gd2 = imagecreatetruecolor(3, 2); + $core = new Core([new Frame($gd1)]); + $this->assertEquals(1, $core->count()); + $result = $core->add(new Frame($gd2)); + $this->assertEquals(2, $core->count()); + $this->assertInstanceOf(Core::class, $result); + } + public function testSetNative(): void { $gd1 = imagecreatetruecolor(3, 2); diff --git a/tests/Drivers/Imagick/CoreTest.php b/tests/Drivers/Imagick/CoreTest.php index 8ea2101b..71859280 100644 --- a/tests/Drivers/Imagick/CoreTest.php +++ b/tests/Drivers/Imagick/CoreTest.php @@ -17,6 +17,17 @@ public function testConstructor(): void $this->assertInstanceOf(Core::class, $core); } + public function testAdd(): void + { + $imagick = new Imagick(); + $imagick->newImage(100, 100, new ImagickPixel('red')); + $core = new Core($imagick); + $this->assertEquals(1, $core->count()); + $result = $core->add(new Frame(clone $imagick)); + $this->assertEquals(2, $core->count()); + $this->assertInstanceOf(Core::class, $result); + } + public function testCount(): void { $imagick = new Imagick();