php GD创建一个透明的png图像 [英] php GD create a transparent png image

查看:102
本文介绍了php GD创建一个透明的png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个透明的png图像并将各种其他png和jpgs分层,以创建一个具有透明度的最终png。我在创建我的初始空透明png时遇到了麻烦。它目前有白色背景。

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


推荐答案

设置 imagealphablending($ image ,true); 在每个新图层上。

试试这个:

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

?>

这篇关于php GD创建一个透明的png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆