简体   繁体   中英

Transparent background with GD in PHP

I am trying to create a chart with PHP and 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). Is it possible to do this with GD? I have searched a lot on the Internet but can't find any solutions.

显示锯齿问题的图像

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" />

The PHP manual basically states that you cannot:

It does not support alpha components. 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.

http://es.php.net/imageantialias

So unless someone comes with third-party code or some witty hacks, you're out of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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