0

When using examples from other posts to try and merge one PNG that has transparent parts on it with another non-transparent PNG, the foreground PNGs transparency is lost and defaults to white.

The code so far:

$width = 349;
$height = 250;

$base_image = imagecreatefrompng($_GET['bg']);
$top_image = imagecreatefrompng($_GET['fg']);
$merged_image = "merged.png";

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

imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, $merged_image);

Can anyone suggest where I may be going wrong?

Coming out like this Coming out like this Should look like this Should look like this

Gga
  • 4,311
  • 14
  • 39
  • 74

2 Answers2

0

Copy from Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Codes should be like this:

imagesavealpha($base_image, true);
imagealphablending($base_image, false);
Community
  • 1
  • 1
silverfox
  • 5,254
  • 1
  • 21
  • 26
0
 $image = imagecreatefrompng($_GET['bg']);
 $frame = imagecreatefrompng($_GET['fg']);

 imagealphablending($frame,true);
 imagecopymerge($image, $frame, 0, 0, 0, 0, 0, 100, 100);

 # Save the image to a file
 imagepng($image, 'file-xyz.png');
Gga
  • 4,311
  • 14
  • 39
  • 74