如何创建具有透明背景的图像 [英] How to create an image with transparent background

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

问题描述

如何使用具有透明背景的GDlib创建图像?

How to create an image with GDlib with a transparent background?

header('content-type: image/png');

$image = imagecreatetruecolor(900, 350);

imagealphablending($image, true);
imagesavealpha($image, true);

$text_color = imagecolorallocate($image, 0, 51, 102);
imagestring($image,2,4,4,'Test',$text_color);

imagepng($image);
imagedestroy($image);

这里的背景是黑色的

推荐答案

您必须使用imagefill(),并用alpha设置为0的分配颜色(imagecolorallocatealpha())填充该颜色.

You have to use imagefill() and fill that with allocated color (imagecolorallocatealpha()) that have alpha set to 0.

正如@mvds所说,没有必要分配",如果它是真彩色图像(24或32位),则它只是一个整数,因此您可以将该整数直接传递给imagefill().

As @mvds said, "allocating isn't necessary", if it is a truecolor image (24 or 32bit) it is just an integer, so you can pass that integer directly to imagefill().

当您调用imagecolorallocate()时,PHP在后台处理真彩色图像的方法是一样的-它只返回该计算出的整数.

What PHP does in the background for truecolor images when you call imagecolorallocate() is the same thing - it just returns that computed integer.

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

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