Search code examples
phpimagepnggdlib

Placing a Transparent PNG onto another Transparent PNG (Bottom Image not showing)


I have two images. I am putting imageA over imageB. However this is causing the bottom image (imageB) colors to not show. Instead the transparent part of imageA is overriding imageB.

$dest = imagecreatefrompng(6.png'); (96x96)
$src = imagecreatefrompng(5.png');

imagealphablending($dest, true);
imagesavealpha($dest, true);

imagealphablending($src, true);
imagesavealpha($src, true);

imagecopy($dest, $src, 10, 30, 0, 0, 40, 40);

ob_start();
imagepng($dest);
$imgswap = ob_get_clean();
imagedestroy($dest);

https://i.sstatic.net/kzK3R.png //img here (I don't have enough reputation to direct link)

As you can see the transparent (white pixels in this example for clarity) are going over marios face. Any ideas?


Solution

  • From docs (https://www.php.net/manual/en/function.imagecolortransparent.php): "Transparency is copied only with imagecopymerge() and true color images, not with imagecopy() or pallete images."

    Try: https://www.php.net/manual/en/function.imagecopymerge.php even though it says in the comments it doesn't support aplha.