Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php GD create a transparent png image

Tags:

I'm trying to create a transparent png image and layer various other pngs and jpgs to create a final png with transparency. I'm having trouble creating my initial empty transparent png. It currently has a white background.

Can anyone point me in the right direction. This is my code so far...

$image = imagecreatetruecolor(485, 500); imagealphablending($image, false); imagesavealpha($image, true); $col=imagecolorallocatealpha($image,255,255,255,127); imagefill($image, 0, 0, $col); //imagefilledrectangle($image,0,0,485, 500,$col);   /* add door glass */ $img_doorGlass = imagecreatefrompng("glass/$doorStyle/$doorGlass.png");      imagecopyresampled($image, $img_doorGlass, 106, 15, 0, 0, 185, 450, 185, 450);                 /* add door */ $img_doorStyle = imagecreatefrompng("door/$doorStyle/$doorStyle"."_"."$doorColor.png");      imagecopyresampled($image, $img_doorStyle, 106, 15, 0, 0, 185, 450, 185, 450);   $fn = md5(microtime()."door_builder").".png";  if(imagepng($image, "user_doors/$fn", 1)){   echo "user_doors/$fn"; }        imagedestroy($image);        
like image 783
michael Avatar asked May 24 '11 11:05

michael


People also ask

How do I make a transparent PNG in HTML?

Transparency is not done in HTML, but is a part of the image itself. The browser will see the image as a PNG and display it as a PNG automatically. To add transparency to the image, you will have to edit the file with a graphics editor like Photoshop.

How do I create a transparent image?

On the Picture Format tab, select Color, and then select Set Transparent Color. Click the color in the picture or image that you want to make transparent. Note: You can't make more than one color in a picture transparent.


1 Answers

Set imagealphablending($image,true); on each new layer.

Try this:

<?php $image = imagecreatetruecolor(485, 500); imagealphablending($image, false); $col=imagecolorallocatealpha($image,255,255,255,127); imagefilledrectangle($image,0,0,485, 500,$col); imagealphablending($image,true);  /* add door glass */ $img_doorGlass = imagecreatefrompng("glass/$doorStyle/$doorGlass.png"); imagecopyresampled($image, $img_doorGlass, 106, 15, 0, 0, 185, 450, 185, 450); imagealphablending($image,true);  /* add door */ $img_doorStyle = imagecreatefrompng("door/$doorStyle/$doorStyle"."_"."$doorColor.png"); imagecopyresampled($image, $img_doorStyle, 106, 15, 0, 0, 185, 450, 185, 450); imagealphablending($image,true);  $fn = md5(microtime()."door_builder").".png";  imagealphablending($image,false); imagesavealpha($image,true); if(imagepng($image, "user_doors/$fn", 1)){     echo "user_doors/$fn"; } imagedestroy($image);  ?> 
like image 191
Lawrence Cherone Avatar answered Sep 29 '22 21:09

Lawrence Cherone