here is my last creation, with this droplet you can create a ptruecolor png with antialiased text on a transparent background (in a very tricky way), the font need you to create a "fonts" folder in your media directory, in this folder you must put a .ttf or .otf file, and you must specify the font by a parameter while calling the droplet.
There is also a short but efficient caching system.
Here is the droplet
Name:
Text2png
Code:
$cache_dir = WB_PATH.'/temp/';
$fontfile = WB_PATH.'/media/fonts/'.$ttffile;
$fontangle = 0;
if (!isset($fontsize)) $fontsize = "12";
if (!isset($fontcolor)) $fontcolor = "000000";
$cache_file = 'text2png_'.md5($text.$fontfile.$fontsize.$fontcolor).'.png';
if (!file_exists($cache_dir.$cache_file))
{$box = @imagettfbbox($fontsize, $fontangle, $fontfile, $text);
$textwidth = abs($box[4] - $box[0]);
$textheight = abs($box[5] - $box[1]);
$imagewidth = $textwidth+10;
$imageheight = $textheight+10;
$xcord = ($imagewidth/2)-($textwidth/2)-2;
$ycord = ($textheight/2)-2;
$img = imagecreatetruecolor($imagewidth, $imageheight);
imagealphablending($img, false);
imagesavealpha($img, true);
imageantialias($img, true);
$opaque = imagecolorallocatealpha($img, 0, 0, 0, 0);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefilledrectangle($img, 0,0, $imagewidth, $imageheight, $transparent);
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $fontcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] ); $textgreen = hexdec( $textrgb[2] ); $textblue = hexdec( $textrgb[3] );}
$opaque = imagecolorallocate($img, $textred, $textgreen, $textblue);
imagettftext($img, $fontsize, $fontangle, $xcord, $ycord, $opaque , $fontfile, $text);
// invert alpha channel
for($i=0;$i<$xsize;$i++)
{for($j=0;$j<$ysize;$j++)
{$color = imagecolorat($img, $i, $j);
$color = ($color & 0x00FFFFFF) | ((127-($color>>24))<<24);
imagesetpixel($img, $i, $j, $color);
}
}
imagepng($img, $cache_dir.$cache_file);
imagedestroy($img);
}
return '<img src="'.WB_URL.'/temp/'.$cache_file.'" style="border:0px;margin:0px;padding:0px;vertical-align:middle;" alt="'.$text.'"/>';
Comments:
Use: [[Text2png?text=(text, Mandatory)&ttffile=(fontfile.ttf, Mandatory)&fontcolor=(fontcolor, Optional)&fontsize=(fontsize, Optional)]]
text = Text to convert into truecolor png with transparent background.
ttffile = True Type Font file (.ttf or .otf), must be in the folder media/fonts/ but you can change this in the code.
fontcolor = Color in 6 digit HEX format, "000000"(black) by default.
fontsize = Size of the font in points (if gd2 is used, otherwise in pixels), 12 by default.
This droplet has a rudimentary but efficient caching system but it never delete any file, if you want you can delete all the png files starting with the name "text2png_" in your temp folder, droplet will recreate the needed files.