PHP font library

In the thortypetools project I’ve been experimenting with different font libraries for extracting glyphs and other information about a font so I can programatically create proofs, previews, JSON-documents and other useful files to display on my thortype website.

Today I’ve worked on using the PHP GD library because this is what DaFont uses to test and create their previews. Around three years ago I submitted a font and with the tidbit of information that they used this library I was able to test my fonts similarly to how they tested theirs.

I had to dig up the old code that turned out to be hanging around on my webserver.

For future reference, and to share what I’ve learned myself I thought I’d just share the code I used to test back then.

// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(2800, 500);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2799, 499, $white);

// The text to draw
$text = 'the quick brown fox jumps over the lazy dog !?()';
$text3 = 'aVVAWTLTOPjgi VyYv Toqueville 01234567890 ! A?';
// Replace path by your own font path
// $font = './TaylorSerif.otf';
$font = './Snufkin.otf';

// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 60, 0, 10, 80, $black, $font, $text);
imagettftext($im, 60, 0, 10, 200, $black, $font, $text2);
imagettftext($im, 60, 0, 10, 300, $black, $font, $text3);

// Using imagepng() results in clearer text compared with imagejpeg()

It’s surprisingly simple, which is one thing I really like about PHP. The language has its limitations, but it’s very good at doing the things it does well.