使用 GD 实现背景图像的透明度
使用GD2用纯色填充透明png,这是我的代码和结果。 基本上,一旦透明度开始,填充颜色就会突然停止,而不是与透明度混合。
private function GenerateImage()
{
$original = imagecreatefrompng($this->ImagePath());
$x = imagesx($original);
$y = imagesy($original);
$image = imagecreate($x,$y);
imagealphablending($image,false);
imagesavealpha($image,true);
imagecopyresampled($image,$original,0,0,0,0,$x,$y,$x,$y);
$colour = imagecolorallocate($image,$this->RGB[0],$this->RGB[1],$this->RGB[2]);
imagefill($image,0,0,$colour);
return imagepng($image,$this->GeneratedPath());
imagedestroy($original);
imagedestroy($image);
}
原始图片:
替代文本 http://far.id.au/jkf/so/blank .png
结果图像:
Using GD2 to fill in a transparent png with a solid colour, here is my code and the result. Basically as soon as the transparency begins, the fill colour stops abruptly instead of blending in with the transparency.
private function GenerateImage()
{
$original = imagecreatefrompng($this->ImagePath());
$x = imagesx($original);
$y = imagesy($original);
$image = imagecreate($x,$y);
imagealphablending($image,false);
imagesavealpha($image,true);
imagecopyresampled($image,$original,0,0,0,0,$x,$y,$x,$y);
$colour = imagecolorallocate($image,$this->RGB[0],$this->RGB[1],$this->RGB[2]);
imagefill($image,0,0,$colour);
return imagepng($image,$this->GeneratedPath());
imagedestroy($original);
imagedestroy($image);
}
Original image:
alt text http://far.id.au/jkf/so/blank.png
Resulting image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的做法是错误的,如果你试图让透明图像出现在颜色之上,那么你需要先填充然后复制图像。
另外,如果您正在使用透明度,则需要调用 imagecreatetruecolor(); 而不是 imagecreate();
如果您尝试在图像顶部绘制红色,请使用 imagefilledrectangle() 而不是 imagefill()。 由于某种原因,图像填充似乎不适用于透明胶片。
I think you are going at it the wrong way, if you are trying to have the transparent image appear on top of the colour then you need to fill first then copy the image.
Also if you are working with transparency you need to call imagecreatetruecolor(); instead of imagecreate();
If you are trying to draw the red on top of the image then use imagefilledrectangle() instead of imagefill(). For some reason imagefill doesn't seem to work well with transparencies.