Skip to content

Commit

Permalink
Add option to add interlaced frames with builder
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Mar 26, 2024
1 parent b191c3e commit 3a2b5f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,16 @@ public function setLoops(int $loops): self
* @param float $delay time delay in seconds
* @param int $left position offset in pixels from left
* @param int $top position offset in pixels from top
* @param bool $interlaced
* @return Builder
*/
public function addFrame(string $source, float $delay = 0, int $left = 0, int $top = 0): self
{
public function addFrame(
string $source,
float $delay = 0,
int $left = 0,
int $top = 0,
bool $interlaced = false
): self {
$frame = new FrameBlock();
$source = Decoder::decode($source);

Expand All @@ -113,7 +119,7 @@ public function addFrame(string $source, float $delay = 0, int $left = 0, int $t

// store image
$frame->setTableBasedImage(
$this->buildTableBasedImage($source, $left, $top)
$this->buildTableBasedImage($source, $left, $top, $interlaced)
);

// add frame
Expand Down Expand Up @@ -156,10 +162,15 @@ protected function buildGraphicControlExtension(
* @param GifDataStream $source
* @param int $left
* @param int $top
* @param bool $interlaced
* @return TableBasedImage
*/
protected function buildTableBasedImage(GifDataStream $source, int $left, int $top): TableBasedImage
{
protected function buildTableBasedImage(
GifDataStream $source,
int $left,
int $top,
bool $interlaced
): TableBasedImage {
$block = new TableBasedImage();
$block->setImageDescriptor(new ImageDescriptor());

Expand All @@ -183,6 +194,9 @@ protected function buildTableBasedImage(GifDataStream $source, int $left, int $t
// set position
$block->getImageDescriptor()->setPosition($left, $top);

// set interlaced flag
$block->getImageDescriptor()->setInterlaced($interlaced);

// add image data from source
$block->setImageData($source->getFirstFrame()->getImageData());

Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,18 @@ public function testAddFrame(): void
$this->assertEquals(25, $gif->getFirstFrame()->getGraphicControlExtension()->getDelay());
$this->assertEquals(1, $gif->getFirstFrame()->getImageDescriptor()->getLeft());
$this->assertEquals(2, $gif->getFirstFrame()->getImageDescriptor()->getTop());
$this->assertFalse($gif->getFirstFrame()->getImageDescriptor()->isInterlaced());
}

public function testAddFrameInterlace(): void
{
$builder = Builder::canvas(320, 240);
$result = $builder->addFrame($this->getTestImagePath('red.gif'), 0.25, 1, 2, true);
$this->assertInstanceOf(Builder::class, $result);
$gif = $builder->getGifDataStream();
$this->assertEquals(25, $gif->getFirstFrame()->getGraphicControlExtension()->getDelay());
$this->assertEquals(1, $gif->getFirstFrame()->getImageDescriptor()->getLeft());
$this->assertEquals(2, $gif->getFirstFrame()->getImageDescriptor()->getTop());
$this->assertTrue($gif->getFirstFrame()->getImageDescriptor()->isInterlaced());
}
}

0 comments on commit 3a2b5f8

Please sign in to comment.