Search code examples
phpgdimagecreatefrompng

PHP PNG image creation outputting wrong colors


I am trying to put an png overlay over an underlying png file. The script works just fine, but instead of giving me the overlay colored as it should be, it is outputting it in gray color. But only the overlay image is gray (bg-color of the overlay, which is basically a border for the underlying image: RGB 20,114,158). And it's 24-bit from PS. The transparency part (white) works just fine.

<?php
$im = imagecreatefrompng($sourceFile);
$overlay = imagecreatefrompng($overlayFile);
$white = imagecolorallocate($overlay, 255, 255, 255);
imagecolortransparent($overlay, $white);
imagecopymerge($im, $overlay, 0, 0, 0, 0, 173, 173,100); 
header('Content-Type: image/png'); 
imagepng($im);
?>

Any help is much appreciated!

Cheers

Chris


Solution

  • You should just using imagecopy() (you use 100 for the last argument ($pct) of imagecopymerge()).

    pct
    The two images will be merged according to pct which can range from 0 to 100. When pct = 0, no action is taken, when 100 this function behaves identically to imagecopy() for pallete images, except for ignoring alpha components, while it implements alpha transparency for true colour images.

    And don't forget to enable imagealphablending() on both images.