How to create an image with GDlib with a transparent background?
header('content-type: image/png');
$image = imagecreatetruecolor(900, 350);
imagealphablending($image, true);
imagesavealpha($image, true);
$text_color = imagecolorallocate($image, 0, 51, 102);
imagestring($image,2,4,4,'Test',$text_color);
imagepng($image);
imagedestroy($image);
Here the background is black
You have to use
imagefill()and fill that with allocated color (imagecolorallocatealpha()) that have alpha set to 0.As @mvds said, “allocating isn’t necessary”, if it is a truecolor image (24 or 32bit) it is just an integer, so you can pass that integer directly to
imagefill().What PHP does in the background for truecolor images when you call
imagecolorallocate()is the same thing – it just returns that computed integer.