2016-09-08 18 views
1

我试图使图像透明使用来自PHP的GD库,但运行下面的代码,只有一部分将是透明的。我试图使图像透明使用来自PHP库的GD库,但运行下面的代码,只有一部分将是透明的

$image = imagecreatefrompng("$second"); 
imagealphablending($image, false); 
$col_transparent = imagecolorallocatealpha($image, 0, 0, 0, 127); 
imagefill($image, 0, 0, $col_transparent); // set the transparent colour as the background. 
imagecolortransparent ($image, $col_transparent); // actually make it transparent 
imagesavealpha($image, TRUE); 
header('Content-Type: image/png'); 
imagepng($image); 

这里有原始图像:https://postimg.org/image/y68nw57z1/
这是产生的图像:https://postimg.org/image/o4n3t6ic7/

正如你所看到的,存在从保持白色所产生的图像部分。
我该如何解决这个问题?

回答

0

您是用填充图像替换图像的白色像素,但对于完全被非白色像素包围的像素(如任何绘画程序)不起作用。相反,您可以修改颜色白色的定义以使其透明:

$image = imagecreatefrompng($second); 
imagetruecolortopalette($image, false, 255); 
$index = imagecolorclosest($image, 255, 255, 255); // find index of white. 
imagecolorset($image, $index, 0, 0, 0, 127); // replace white with transparent black. 
header('Content-Type: image/png'); 
imagepng($image); 
+0

工作。谢谢tim – Louie

+0

不客气。请注意,[礼仪](http://stackoverflow.com/help/someone-answers)将接受为您的问题提供最佳解决方案的答案。 – timclutton

+0

存在提高图像质量的模式? – Louie