简体   繁体   English

php GD 创建透明png图片

[英]php GD create a transparent png image

I'm trying to create a transparent png image and layer various other pngs and jpgs to create a final png with transparency.我正在尝试创建一个透明的 png 图像并将各种其他 png 和 jpg 分层以创建具有透明度的最终 png。 I'm having trouble creating my initial empty transparent png.我在创建初始的空透明 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);       

Set imagealphablending($image,true);设置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);

?>

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

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

and try to uncomment imagefilledrectangle line.并尝试取消注释imagefilledrectangle线。
I can test this code - give me the pictures:)我可以测试这段代码 - 给我图片:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM