Page 1 of 1

how to create antialiased (truecolor?) transparent PNG

Posted: Sun Oct 28, 2007 7:34 pm
by blazenko
hi

i'm new to GD and i can't figure out how to create a PNG with transparent background, but with fg color antialiasing into the transparent bg...

i need a image created with ttf font

i can only seem to get it either aliased (i.e. GIF-like one-color transparency), or truecolor antialiased but with black background...

what i need sounds to me as a normal request but i just can't figure it out...

i found this http://www.phpgd.com/scripts.php?script=27 script with a promising function name (imagecreatetruecolortransparent) but i don't understand how to replace the hardcoded blank png with what i need (ttf text)

can someone explain in simple terms how to have what i need?

thanx

Posted: Mon Oct 29, 2007 8:17 am
by Rovas
First create the text, second create an colored rectangle, extract the color from rectangle and make it transparent.
Use this script as an example (it' s not mine, it' s author gave it on another forum):

Code: Select all

// Create the image
$size = imagettfbbox($fontsize, 0, $font, $text);
//$width = $size[2] + $size[0] + 8;
//$height = abs($size[1]) + abs($size[7]);
$width = "576";
$height = "350";
$im = imagecreate($width, $height);

$colourBlack = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $colourBlack);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Add the text
imagefttext($im, $fontsize, 0, 0, abs($size[5]), $black, $font, $text);

// Using imagepng() results in clearer text compared with imagegif
imagepng($im);
imagedestroy($im);

Re: how to create antialiased (truecolor?) transparent PNG

Posted: Thu Jan 08, 2009 3:56 am
by HerrSerker
The former gives you ragged outlines with the background color:

Use the function imagecreatetruecolortransparent at this url
http://www.phpgd.com/scripts.php?script=27&rating=20