Question:
Php: merging two png images, preserving transparency?
?
2011-12-21 04:09:55 UTC
The png images have parts transparent. When merging them using imagecopy(), or imagecopymerge(), or any other function, the last image overwrites the image of the bottom.
What should be the sequence of commands?

// create a blank image:
$avatar_img = imagecreatetruecolor (AVATARWIDTH, AVATARHEIGHT); // blank image
$trp = imagecolorallocate($avatar_img, 0, 0, 0);
ImageColorTransparent($avatar_img, $trp);
ImageAlphaBlending($avatar_img, false);
// fill a background:
$grey = imagecolorallocate($avatar_img, 200, 200, 200);
imagefilledrectangle ($avatar_img, 0, 0, AVATARWIDTH -1, AVATARHEIGHT -1, $grey);
// the image is a plain grey.
// get the image that contains transparencies:
$base_avatar_img = imagecreatefrompng($fname);
imagecolortransparent($base_avatar_img, ImageColorAllocate($base_avatar_img, 0, 0, 0));
ImageAlphaBlending($base_avatar_img, false);
// merge:
imagecopyresampled ($avatar_img, $base_avatar_img, 0, 0, 0, 0, AVATARWIDTH -1, AVATARHEIGHT -1, AVATARWIDTH -1, AVATARHEIGHT -1);
imagepng($avatar_img,"avatar.png");
imagedestroy($avatar_img);
=> the grey background has disappeared...
What have I forgotten?
(same results with imagecopymerge ($avatar_img, $base_avatar_img, 0, 0, 0, 0, AVATARWIDTH -1, AVATARHEIGHT -1, 100);
Four answers:
Eli
2011-12-21 10:03:44 UTC
$image_1 = imagecreatefrompng('image_1.png');

$image_2 = imagecreatefrompng('image_2.png');

imagealphablending($image_1, true);

imagesavealpha($image_1, true);

imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);

imagepng($image_1, 'image_3.png');





or use



http://us3.php.net/manual/en/function.imagick-compositeimage.php



this component



http://www.php.net/manual/en/function.imagick-combineimages.php
?
2016-11-15 08:47:14 UTC
Php Imagecopy
2016-12-01 15:42:15 UTC
Assuming you're saving an instant reproduction of the image (stunning click the image, keep image as) then any advice interior the image, which incorporate the transparency, is easily preserved. How are you checking the image transparency?
2014-04-21 02:20:13 UTC
Thank you! i met a lot of **** post before i read it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...