From 9f5e0624d4b8d073f58f8bfef71ba8363d9fb688 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 11 Aug 2024 12:02:30 +0200 Subject: [PATCH] Fix bug in cloner transparency (#1388) If an image was cloned with cloneEmpty() and a fully transparent color for exampe "rgba(255, 255, 255, 0)" the turned out as white background when encoding formats with only binary transparency like GIF. This patch sets the background color as fully transparent if it has an alpha channel value is below `0.5`. --- src/Drivers/Gd/Cloner.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Drivers/Gd/Cloner.php b/src/Drivers/Gd/Cloner.php index 8edc2023..8723006a 100644 --- a/src/Drivers/Gd/Cloner.php +++ b/src/Drivers/Gd/Cloner.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Gd; use GdImage; +use Intervention\Image\Colors\Rgb\Channels\Alpha; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Geometry\Rectangle; @@ -67,6 +68,12 @@ public static function cloneEmpty( imagealphablending($clone, true); imagesavealpha($clone, true); + // set background image as transparent if alpha channel value if color is below .5 + // comes into effect when the end format only supports binary transparency (like GIF) + if ($background->channel(Alpha::class)->value() < 128) { + imagecolortransparent($clone, $processor->colorToNative($background)); + } + return $clone; }