2012-04-03 72 views
4

我正在尝试使用PHP和GD创建图表。图表已经可以工作,但是我的 无法使背景透明(带有抗锯齿功能)。在PHP中使用透明背景与GD

我想在渐变背景(这是HTML背景)上放置一个抗锯齿线,但它显示了一些白色像素(请参阅下面的图像链接和我的代码)。 GD可以做到这一点吗?我在互联网上搜索了很多,但找不到任何解决方案。

Image showing aliasing problem

PHP

<?php 
$img = imagecreatetruecolor(1000, 1000); 

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

$color = imagecolorallocatealpha($img, 255, 255, 255, 127); 
$red = imagecolorallocate($img, 255, 0, 0); 

imagefill($img, 0, 0, $color); 

imageantialias($img, true); 
imageline($img, 10, 10, 500, 20, $red); 

header("content-type: image/png"); 
imagepng($img); 
imagedestroy($img); 
?> 

HTML

<style type="text/css"> 
    body{ 
     background:url('/Image/background.png'); 
    } 
</style> 

<img src="./example.php" /> 
+1

我的经验是GD在抗锯齿方面很糟糕,你有没有考虑过使用ImageMagick? – 2012-04-03 15:30:32

+0

不是真的(因为我的图表构建器几乎在GD中完成)。那里的反别名更好吗? – Marcel 2012-04-04 08:05:55

回答

1

PHP手册基本规定,你不能:

它不支持Alpha合作mponents。它使用直接混合 操作。它只适用于真彩色图像。

不支持厚度和样式。

使用具有透明背景颜色的抗锯齿基元可能会结束 并带来一些意外结果。混合方法使用背景 颜色作为任何其他颜色。缺少对alpha组件的支持, 不支持基于alpha的抗混叠方法。

http://es.php.net/imageantialias

所以,除非有人来与第三方代码或一些诙谐的黑客,你的运气了。

+0

奥克感谢您的帮助!然后我会寻找另一个解决方案。 – Marcel 2012-04-03 17:17:21