php GD create a transparent png image

41,906

Solution 1

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);

?>

Solution 2

This code worked for me:

$img=imagecreatetruecolor(180,20);
imagealphablending($img,false);

$col=imagecolorallocatealpha($img,255,255,255,127);
imagefilledrectangle($img,0,0,180,20,$col);
imagealphablending($img,true);

$font=$_SERVER["DOCUMENT_ROOT"].'/fonts/Arial.ttf';
$color = imagecolorallocate($img, 140, 173, 209);
imagettftext($img,11,0,5,14,$color,$font,'Text goes here'); 

header('Content-Type: image/png');
imagealphablending($img,false);
imagesavealpha($img,true);
imagepng($img);
Share:
41,906

Related videos on Youtube

michael
Author by

michael

Updated on December 11, 2020

Comments

  • michael
    michael over 3 years

    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);       
    
    • ashein
      ashein about 13 years
      does the change in blending mode help? Don't have the images to test myself, but it would seem that the png images should blend. Otherwise the image is created just as per php docs and user-contributed notes, so can't see the problem there, sorry
  • michael
    michael about 13 years
    not 100% sure why, but this works. thanks for your help Lawrence
  • Lawrence Cherone
    Lawrence Cherone about 13 years
    np, it works because imagealphablending($image,true); is set to true on every new png added to the layer then lastly imagesavealpha($image,true); before save, you had it at the top. sometimes it will get reset