Skip to content

Commit

Permalink
Add option to encode interlaced GIF format
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Mar 26, 2024
1 parent f5e243e commit 53bc036
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Drivers/Gd/Encoders/GifEncoder.php
Expand Up @@ -23,7 +23,9 @@ public function encode(ImageInterface $image): EncodedImage

$gd = $image->core()->native();
$data = $this->buffered(function () use ($gd) {
imageinterlace($gd, $this->interlaced);
imagegif($gd);
imageinterlace($gd, false);
});

return new EncodedImage($data, 'image/gif');
Expand Down
4 changes: 4 additions & 0 deletions src/Drivers/Imagick/Encoders/GifEncoder.php
Expand Up @@ -24,6 +24,10 @@ public function encode(ImageInterface $image): EncodedImage
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);

if ($this->interlaced) {
$imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
}

return new EncodedImage($imagick->getImagesBlob(), 'image/gif');
}
}
2 changes: 1 addition & 1 deletion src/Encoders/GifEncoder.php
Expand Up @@ -8,7 +8,7 @@

class GifEncoder extends SpecializableEncoder
{
public function __construct()
public function __construct(public bool $interlaced = false)
{
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Drivers/Gd/Encoders/GifEncoderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Tests\Unit\Drivers\Gd\Encoders;

use Intervention\Gif\Decoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Encoders\GifEncoder;
Expand All @@ -20,5 +21,19 @@ public function testEncode(): void
$encoder = new GifEncoder();
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertFalse(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}

public function testEncodeInterlaced(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new GifEncoder(interlaced: true);
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertTrue(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Drivers/Imagick/Encoders/GifEncoderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;

use Intervention\Gif\Decoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Encoders\GifEncoder;
Expand All @@ -20,5 +21,19 @@ public function testEncode(): void
$encoder = new GifEncoder();
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertFalse(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}

public function testEncodeInterlaced(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new GifEncoder(interlaced: true);
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertTrue(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}
}

0 comments on commit 53bc036

Please sign in to comment.