How to draw text as a graphic and make dynamic buttons or hit counters.
For built-in GD fonts, use ImageString():
| PHP Code |
|---|
ImageString($image, 1, $x, $y, 'Text', $text_color); |
| Select all |
For TrueType fonts, use ImageTTFText():
| PHP Code |
|---|
ImageTTFText($image, $size, 0, $x, $y, $text_color, '/path/to/font.ttf', 'Text'); |
| Select all |
For PostScript Type 1 fonts, use ImagePSLoadFont() and ImagePSText():
| PHP Code |
|---|
$font = ImagePSLoadFont('/path/to/font.pfb');
ImagePSText($image, 'Text', $font, $size, text_color, $background_color, $x, $y);
|
| Select all |
Call ImageString() to place text onto the canvas. Like other GD drawing functions, ImageString() needs many inputs: the image to draw on, the font number, the x and y coordinates of the upper right position of the first characters, the text string to display, and finally, the color to use to draw the string.
With ImageString(), there are five possible font choices, from 1 to 5. Font number 1 is the smallest, while font 5 is the largest
To draw text vertically instead of horizontally, use the function ImageStringUp() instead:
| PHP Code |
|---|
ImageStringUp($image, 1, $x, $y, 'Text', $text_color); |
| Select all |
To use TrueType fonts, you must also install the FreeType library and configure PHP during installation to use FreeType. The FreeType main site is http://www.freetype.org. To enable FreeType 1.x support, use --with-ttf and for FreeType 2.x, pass --with-freetype-dir=DIR.
Like ImageString(), ImageTTFText() prints a string to a canvas, but it takes slightly different options and needs them in a different order:
| PHP Code |
|---|
ImageTTFText($image, $size, $angle, $x, $y, $text_color, '/path/to/font.ttf',$text); |
| Select all |
The $size argument is the font size in pixels; $angle is an angle of rotation, in degrees going counterclockwise; and /path/to/font.ttf is the pathname to the TrueType font file. Unlike ImageString(), ($x,$y) are the lower left coordinates of the baseline for the first character. (The baseline is where the bottom of most characters sit. Characters such as "g" and "j" extend below the baseline; "a" and "z" sit on the baseline.)
PostScript Type 1 fonts require t1lib to be installed, which can be downloaded from ftp://sunsite.unc.edu/pub/Linux/libs/graphics/ and built into PHP using --with-t1lib.
Again, the syntax for printing text is similar but not the same:
| PHP Code |
|---|
$font = ImagePSLoadFont('/path/to/font.pfb');
ImagePSText($image, $text, $font, $size, $text_color, $background_color, $x, $y);
ImagePSFreeFont($font);
|
| Select all |
First, PostScript font names can't be directly passed into ImagePSText(). Instead, they must be loaded using ImagePSLoadFont(). On success, the function returns a font resource usable with ImagePSText(). In addition, besides specifying a text color, you also pass a background color to be used in antialiasing calculations. The ($x,$y) positioning is akin to the how the TrueType library does it. Last, when you're done with a font, you can release it from memory by calling ImagePSFreeFont().
Besides the mandatory arguments listed above, ImagePSText() also accepts four optional ones, in this order: space, tightness, angle, and antialias_steps. You must include all four or none of the four (i.e., you can't pass one, two, or three of these arguments). The first controls the size of a physical space (i.e., what's generated by hitting the Space bar); the second is the tightness of the distance between letters; the third is a rotation angle, in degrees, counterclockwise; and the last is an antialiasing value. This number must be either 4 or 16. For better-looking, but more computationally expensive graphics, use 16 instead of 4.
By default, space, tightness, and angle are all 0. A positive number adds more space between words and letters or rotates the graphic counterclockwise. A negative number tightens words and letters or rotates in the opposite direction.
| PHP Code |
|---|
// normal image ImagePSText($image, $text, $font, $size, $black, $white, $x, $y, 0, 0, 0, 4); // extra space between words ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 30, 100, 0, 0, 4); // extra space between letters ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 60, 0, 100, 0, 4); |
| Select all |
$PHP $is $a $language $for $pretend $programmers.
$Gregor $is $a $name $for $strange $boys.
Discuss