Page 274 - Open Soource Technologies 304.indd
P. 274
Web Technologies-I
Notes 11.5 Image with Text
For built-in GD fonts, use ImageString( ):
ImageString($image, 1, $x, $y, ‘My India’, $text_color);
For TrueType fonts, use ImageTTFText( ):
ImageTTFText($image, $size, 0, $x, $y, $text_color, ‘/path/to/font.ttf’, ‘My India’);
For PostScript Type 1 fonts, use ImagePSLoadFont( ) and ImagePSText( ):
$font = ImagePSLoadFont(‘/path/to/font.pfb’); ImageString($image, ‘My India’, $font, $size,
$text_color, $background_color, $x, $y);
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 colour 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, as shown in Figure 11.7. Anything above or below that range
generates a size equivalent to the closest legal number.
Figure 11.7: Built-in GD Font Sizes
The quick brown for jumps over the lazy dog.
The quick brown for jumps over the lazy dog.
The quick brown for jumps over the lazy dog.
The quick brown for jumps over the lazy dog.
The quick brown for jumps over the lazy dog.
To draw text vertically instead of horizontally, use the function ImageStringUp( ) instead.
Figure 11.8 shows the output.
ImageStringUp($image, 1, $x, $y, ‘My India’, $text_color);
Figure 11.8: Vertical Text
M
y
I
n
d
i
a
To use TrueType fonts, you must also install the FreeType library and configure PHP during
installation to use FreeType. The FreeType main site ishttp://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:
ImageTTFText($image, $size, $angle, $x, $y, $text_color, ‘/path/to/font.ttf’, $text);
268 LOVELY PROFESSIONAL UNIVERSITY