0

Friends i want to generate one png image from multiple transparent PNG image but the issue is that i can generate only last image

Both images can not combine.

My code is given below

$x = 363;
$y = 267;

$im_dest = imagecreatetruecolor ($x, $y);
imagealphablending($im_dest, false);

$im = imagecreatefrompng('2.png');
$im1 = imagecreatefrompng('1.png');

imagecopy($im_dest, $im1, 0, 0, 0, 0, $x, $y);
imagecopy($im_dest, $im, 0, 0, 0, 0, $x, $y);

imagesavealpha($im_dest, true);
imagepng($im_dest, 'small_redfade.png');

These are the images which i am using to join in single image

http://s11.postimg.org/h6lui7yjn/image.png

http://s21.postimg.org/o7zdnwcnb/image.png

Munir Vora
  • 51
  • 11

2 Answers2

2

Here is code which works:

$width = 210;
$height = 190;

$layers = array();
$layers[] = imagecreatefrompng("img/01_boy_faceB.png");
$layers[] = imagecreatefrompng("img/01_boy_hairB.png");

$image = imagecreatetruecolor($width, $height);

// to make background transparent
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);

/* if you want to set background color
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
*/

imagealphablending($image, true);
for ($i = 0; $i < count($layers); $i++) {
    imagecopy($image, $layers[$i], 0, 0, 0, 0, $width, $height);
}
imagealphablending($image, false);
imagesavealpha($image, true);

imagepng($image, 'final_img.png');

?>
atulmy
  • 1,364
  • 15
  • 23
1

ImageMagick::Composite can handle this, sadly haven't done in GD so will leave others to explain how to do it there.

Something like:

<?php

$firstImage = new Imagick("firstImage.png");
$secondImage = new Imagick("secondImage.png");

$firstImage->compositeImage($secondImage, Imagick::COMPOSITE_COPYOPACITY, 0, 0 );

header('Content-type: image/png');
echo $firstImage;

?>

This should preserve alpha.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • hi, I downloaded Imagick dll and install it on wamp with all the step i follow but though i got the fatal error that Imagick class not found and first i got that php_imagik.dll is not valid win32 app, Give me suggestion Please – Munir Vora Apr 18 '13 at 13:23
  • i tried this step but ther is some confusing related to put imagemagic binary,, add the ImageMagick binary folder into your PATH mins which Path i should Add – Munir Vora Apr 19 '13 at 05:20
  • PATH is an environment variable which tells Windows where it can load from. Search for ' PATH' on Google to find it, it's just a list of folders for binaries to be read from. – Glitch Desire Apr 19 '13 at 07:57