php 如何创建具有透明背景的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8437665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an image with transparent background
提问by clarkk
How to create an image with GDlib with a transparent background?
如何使用具有透明背景的 GDlib 创建图像?
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);
Here the background is black
这里的背景是黑色的
采纳答案by Dalibor Filus
You have to use imagefill()
and fill that with allocated color (imagecolorallocatealpha()
) that have alpha set to 0.
您必须使用imagefill()
并用分配的颜色 ( imagecolorallocatealpha()
)填充它,这些颜色的alpha 设置为 0。
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()
.
正如@mvds 所说,“不需要分配”,如果它是真彩色图像(24 位或 32 位),则它只是一个整数,因此您可以将该整数直接传递给imagefill()
.
What PHP does in the background for truecolor images when you call imagecolorallocate()
is the same thing - it just returns that computed integer.
当您调用时,PHP 在后台为真彩色图像所做的imagecolorallocate()
事情是一样的 - 它只是返回计算出的整数。
回答by mvds
Add a line
添加一行
imagefill($image,0,0,0x7fff0000);
somewhere before the imagestring
and it will be transparent.
之前的某个地方imagestring
,它将是透明的。
0x7fff0000
breaks down into:
0x7fff0000
分解为:
alpha = 0x7f
red = 0xff
green = 0x00
blue = 0x00
which is fully transparent.
这是完全透明的。
回答by Dejan Marjanovic
Something like this...
像这样的东西...
$im = @imagecreatetruecolor(100, 25);
# important part one
imagesavealpha($im, true);
imagealphablending($im, false);
# important part two
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
# do whatever you want with transparent image
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "captcha.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
回答by Okochea
This should work:
这应该有效:
$img = imagecreatetruecolor(900, 350);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127); //fill transparent back
imagefill($img, 0, 0, $color);
imagesavealpha($img, true);
回答by Faisal Rehman
This should work. It has worked for me.
这应该有效。它对我有用。
$thumb = imagecreatetruecolor($newwidth,$newheight);
$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
imagefill($thumb, 0, 0, $transparent);
imagesavealpha($thumb, true);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb, $output_dir);
回答by Ritesh
Sometime you will not get transparent image due to problems in PNG image. The image should be in one of the following recommended formats:
有时,由于 PNG 图像的问题,您将无法获得透明图像。图像应采用以下推荐格式之一:
PNG-8 (recommended)
Colors: 256 or less
Transparency: On/Off
GIF
Colors: 256 or less
Transparency: On/Off
JPEG
Colors: True color
Transparency: n/a
The imagecopymerge function does not properly handle the PNG-24 images; it is therefore not recommend.
imagecopymerge 函数不能正确处理 PNG-24 图像;因此不推荐。
If you are using Adobe Photoshop to create watermark images, it is recommended that you use "Save for Web" command with the following settings:
如果您使用 Adobe Photoshop 创建水印图像,建议您使用具有以下设置的“保存为 Web”命令:
File Format: PNG-8, non-interlaced
Color Reduction: Selective, 256 colors
Dithering: Diffusion, 88%
Transparency: On, Matte: None
Transparency Dither: Diffusion Transparency Dither, 100%