From 50a16bbdc1361ed6b058200f667fdfdfae82acd9 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 14 Aug 2024 14:57:09 +0200 Subject: [PATCH] Fix lost transparency when converting to GIF format --- phpunit.dist.xml | 3 +++ src/Drivers/Gd/Encoders/GifEncoder.php | 4 ++-- tests/Feature/Gd/ConvertPngGif.php | 22 ++++++++++++++++++++++ tests/Feature/Imagick/ConvertPngGif.php | 22 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Gd/ConvertPngGif.php create mode 100644 tests/Feature/Imagick/ConvertPngGif.php diff --git a/phpunit.dist.xml b/phpunit.dist.xml index d13b0ce82..e70595aa5 100644 --- a/phpunit.dist.xml +++ b/phpunit.dist.xml @@ -4,6 +4,9 @@ ./tests/Unit + + ./tests/Feature + diff --git a/src/Drivers/Gd/Encoders/GifEncoder.php b/src/Drivers/Gd/Encoders/GifEncoder.php index 875df7871..3c0c6e2ce 100644 --- a/src/Drivers/Gd/Encoders/GifEncoder.php +++ b/src/Drivers/Gd/Encoders/GifEncoder.php @@ -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; @@ -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'); diff --git a/tests/Feature/Gd/ConvertPngGif.php b/tests/Feature/Gd/ConvertPngGif.php new file mode 100644 index 000000000..6e5eab7cb --- /dev/null +++ b/tests/Feature/Gd/ConvertPngGif.php @@ -0,0 +1,22 @@ +read( + $this->readTestImage('circle.png')->toGif() + ); + + $this->assertTransparency($converted->pickColor(0, 0)); + $this->assertColor(4, 2, 4, 255, $converted->pickColor(25, 25), 4); + } +} diff --git a/tests/Feature/Imagick/ConvertPngGif.php b/tests/Feature/Imagick/ConvertPngGif.php new file mode 100644 index 000000000..d9bf11bf5 --- /dev/null +++ b/tests/Feature/Imagick/ConvertPngGif.php @@ -0,0 +1,22 @@ +read( + $this->readTestImage('circle.png')->toGif() + ); + + $this->assertTransparency($converted->pickColor(0, 0)); + $this->assertColor(4, 2, 4, 255, $converted->pickColor(25, 25), 4); + } +}