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.

Image showing aliasing problem


$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");


<style type="text/css">

<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.

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