php GD create a transparent png image

前端 未结 3 1019
Happy的楠姐
Happy的楠姐 2020-11-27 06:00

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 tr

相关标签:
3条回答
  • 2020-11-27 06:40

    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);
    
    ?>
    
    0 讨论(0)
  • 2020-11-27 06:55

    Try to replace
    $col=imagecolorallocatealpha($image,255,255,255,127);
    with
    $col=imagecolorallocate($image,255,255,255);

    and try to uncomment imagefilledrectangle line.
    I can test this code - give me the pictures :)

    0 讨论(0)
  • 2020-11-27 07:00

    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);
    
    0 讨论(0)
提交回复
热议问题