How to create an image with transparent background
Something like this...
$im = @imagecreatetruecolor(100, 25);# important part oneimagesavealpha($im, true);imagealphablending($im, false);# important part two$white = imagecolorallocatealpha($im, 255, 255, 255, 127);imagefill($im, 0, 0, $white);# do whatever you want with transparent image$lime = imagecolorallocate($im, 204, 255, 51);imagettftext($im, $font, 0, 0, $font - 3, $lime, "captcha.ttf", $string);header("Content-type: image/png");imagepng($im);imagedestroy($im);
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.