how to create antialiased (truecolor?) transparent PNG

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
blazenko
Forum Newbie
Posts: 1
Joined: Sun Oct 28, 2007 7:26 pm

how to create antialiased (truecolor?) transparent PNG

Post 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
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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);
HerrSerker
Forum Newbie
Posts: 2
Joined: Thu Jan 08, 2009 3:16 am

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

Post 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
Post Reply