Skip to content

Commit

Permalink
Fix bug in cloner transparency (#1388)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
olivervogel authored Aug 11, 2024
1 parent 31779d0 commit 9f5e062
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Drivers/Gd/Cloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 9f5e062

Please sign in to comment.