PHP & imagettftext with Webcore TrueType fonts.

If you’re trying to write some text using PHP/GD & the imagettftext() function, you will of course need some TrueType fonts to work with. I’ve found a great set of fonts available for free: Webcore. Webcore contains all the fonts web designers constantly use, things like Arial, Tahoma, Verdana, Georgia, etc..

To install, simply download to your server and install the RPM (Fedora):
shell> wget
shell> rpm -i webcore-fonts-3.0-1.noarch.rpm

These fonts will now be available to you in /usr/share/fonts/webcore/.

Make sure you specify the full path when calling imagettftext(), eg:
// Set the content-type
header("Content-type: image/png");
// Replace path by your own font path
$font = '/usr/share/fonts/webcore/arial.ttf';
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Hello world!';
// Add some text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()

Your mileage may vary if you are using a different distribution, but the docs state that this RPM should work on other distros.