简体   繁体   English

透明背景与GD在PHP中

[英]Transparent background with GD in PHP

I am trying to create a chart with PHP and GD. 我正在尝试使用PHP和GD创建一个图表。 The chart is already working but I can't get the background to be transparent (with anti-aliasing). 图表已经工作但我不能让背景变得透明(使用抗锯齿)。

I want to place an anti-aliased line on a gradient background (this is the HTML background) but it shows some white pixels (see the image link and my code below). 我想在渐变背景上放置一个消除锯齿的线(这是HTML背景),但它显示了一些白色像素(请参阅下面的图像链接和我的代码)。 Is it possible to do this with GD? 有可能用GD做到这一点吗? I have searched a lot on the Internet but can't find any solutions. 我在互联网上搜索了很多,但找不到任何解决方案。

显示锯齿问题的图像

PHP 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 HTML

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

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

The PHP manual basically states that you cannot: PHP手册基本上说明你不能:

It does not support alpha components. 它不支持alpha组件。 It works using a direct blend operation. 它使用直接混合操作。 It works only with truecolor images. 它仅适用于真彩色图像。

Thickness and styled are not supported. 厚度和样式不受支持。

Using antialiased primitives with transparent background color can end with some unexpected results. 使用具有透明背景颜色的抗锯齿图元可以以一些意想不到的结果结束。 The blend method uses the background color as any other colors. 混合方法使用背景颜色作为任何其他颜色。 The lack of alpha component support does not allow an alpha based antialiasing method. 缺少alpha组件支持不允许基于alpha的抗锯齿方法。

http://es.php.net/imageantialias http://es.php.net/imageantialias

So unless someone comes with third-party code or some witty hacks, you're out of luck. 因此,除非有人带有第三方代码或一些诙谐的黑客,否则你运气不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM