Skip to content

Commit

Permalink
Fix lost transparency when converting to GIF format
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 14, 2024
1 parent 54aa51e commit 50a16bb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<testsuite name="Unit Tests">
<directory suffix=".php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature Tests">
<directory suffix=".php">./tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/Gd/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use Intervention\Gif\Builder as GifBuilder;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\GifEncoder as GenericGifEncoder;
use Intervention\Image\Exceptions\EncoderException;
Expand All @@ -26,11 +27,10 @@ public function encode(ImageInterface $image): EncodedImage
return $this->encodeAnimated($image);
}

$gd = $image->core()->native();
$gd = Cloner::clone($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
22 changes: 22 additions & 0 deletions tests/Feature/Gd/ConvertPngGif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Tests\Feature\Gd;

use Intervention\Image\ImageManager;
use Intervention\Image\Tests\GdTestCase;

class ConvertPngGif extends GdTestCase
{
public function testConversionKeepsTransparency(): void
{
$converted = ImageManager::gd()
->read(
$this->readTestImage('circle.png')->toGif()
);

$this->assertTransparency($converted->pickColor(0, 0));
$this->assertColor(4, 2, 4, 255, $converted->pickColor(25, 25), 4);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Imagick/ConvertPngGif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Tests\Feature\Imagick;

use Intervention\Image\ImageManager;
use Intervention\Image\Tests\ImagickTestCase;

class ConvertPngGif extends ImagickTestCase
{
public function testConversionKeepsTransparency(): void
{
$converted = ImageManager::imagick()
->read(
$this->readTestImage('circle.png')->toGif()
);

$this->assertTransparency($converted->pickColor(0, 0));
$this->assertColor(4, 2, 4, 255, $converted->pickColor(25, 25), 4);
}
}

0 comments on commit 50a16bb

Please sign in to comment.