Skip to content

Commit

Permalink
Add methods add() to cores
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 26, 2023
1 parent c8bc9ca commit 21acb14
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Drivers/Gd/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions src/Drivers/Imagick/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/Interfaces/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Gd/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/Drivers/Imagick/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 21acb14

Please sign in to comment.