Fonts: TrueType

 

Unlike bitmap fonts that are basically collections of pixel-based images of each character drawn one specific size, vector (or outline) fonts describe each character mathematically as points and lines, and a single font file is used to generate text at any size.

Vector fonts come in a potentially confusing array of types, but the PHP image functions (if you have the appropriate libraries) use the two most common: TrueType and PostScript (Type1). TrueType fonts require the FreeType libraries, and PostScript fonts require the currently orphaned T1Lib library. You can find an excellent guide to getting these libraries compiled and working with PHP & GD (including the version bundled in 4.3) in a *NIX environment here.

In addition to the required libraries, you'll also need the actual font files. PostScript fonts are most often require licensing, you can find some free fonts online and included with X. There are many more free TrueType fonts available, and while they are of generally lower quality, for most graphic purposes are fine.

 

TrueType
In order to convert a string of characters using a TrueType font into a pixelated image (a process called rendering), the gd library requires either (or both) the FreeType or FreeType 2 open source font rendering libraries. FreeType uses a patented optimization algorithm that requires a royalty fee to be paid or must be disabled (resulting in lower quality images). The more recent FreeType 2 doesn't suffer from this limitation.

The easiest way to tell if one or both of the libraries are installed on your machine is to check for the freetype.h file:

# locate freetype.h
/usr/include/freetype1/freetype/freetype.h
/usr/include/freetype2/freetype/freetype.h
Rendering with FreeType
array imageTTFText  ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text)
Draws the string text in the image identified by image, starting at coordinates x, y (top left is 0, 0), at an angle of angle in color color, using the TrueType font file identified by fontfile. Depending on which version of the GD library that PHP is using, when fontfile does not begin with a leading '/', '.ttf' will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.
The coordinates given by x, y will define the basepoint of the first character (roughly the lower-left corner of the character).
angle is in degrees, with 0 degrees being left-to-right reading text (3 o'clock direction), and higher values representing a counter-clockwise rotation. (i.e., a value of 90 would result in bottom-to-top reading text).
fontfile is the path to the TrueType font you wish to use.
text is the text string which may include UTF-8 character sequences (of the form: {) to access characters in a font beyond the first 255.
color is the color index. Using the negative of a color index has the effect of turning off antialiasing.
Returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is lower left, lower right, upper right, upper left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontallty.

array imageTTFBBox  ( int size, int angle, string fontfile, string text)
Uses a subset of the parameters for imageTTFText(), and does nothing but returns the same bounding box array that would be returned by imageTTFText().

Rendering with FreeType 2
array imageFTText  ( resource image, int size, int angle, int x, int y, int col, string font_file, string text, array extrainfo )
Write text to the image using fonts using FreeType 2.

array imageFTBBox  ( int size, int angle, string font_file, string text, array extrainfo)
Give the bounding box of a text using fonts via freetype2

Note: the online manual indicates that the extrainfo paramter is optional, but this is not the case. If you don't have any extrainfo to pass to this (as yet undocumented) function, simply use array() instead to pass a blank parameter.