I'm still trying to work out TTF. I can't remember where I got this code from but it works if your ok with system fonts.
Saved as button.php
<?
// button.php4 - used to write text to a png
// header
Header("Content-Type: image/png");
// set up image and colours
$im = ImageCreateFromPNG("images/button.png");
$white = ImageColorAllocate($im, 0, 0,0);
// get font dimensiona
$font_height = ImageFontHeight(3);
$font_width = ImageFontWidth(3);
// get image dimensiona
$image_height = ImageSY($im);
$image_width = ImageSX($im);
// get string length
$length = $font_width * strlen($string);
// calculate start coordinates for string
$image_center_x = ($image_width/2)-($length/2);
$image_center_y = ($image_height/2)-($font_height/2);
// write string
ImageString($im, 5, $image_center_x, $image_center_y, $string, $white);
// output to browser
ImagePNG($im);
?>
to call the image
<img src="/button.php?string=<? echo $stringname; ?>