18.14 Using TrueType Fonts


You want to draw a string using a TrueType font instead of a built-in or PostScript font.

Technique

Use the ImageTTFText() function:

 <?php header("Content-type: image/gif"); $im = ImageCreate(140, 50); $black = ImageColorAllocate($im, 0, 0, 0); $white = ImageColorAllocate($im, 255, 255, 255); ImageTTFText($im, 24, 0, 30, 35, $white, "assets/arial.ttf", "Hello"); ImageGif($im); ImageDestroy($im); ?> 

Comments

The prototype for the ImageTTFText() function looks like this:

 array ImageTTFText(int im, int size, int angle, int x, int y, int col,                   string fontfile, string text) 

Note that for this function, the x and y arguments represent the coordinates of the lower-left corner of the first character in the string.

ImageTTFText() returns an array of eight elements representing the bounding box of the drawn text, starting from upper left and going clockwise. The bounding box points are relative to the text, so if you draw text at a 90-degree angle, "lower left" will actually be in the lower-right corner of the image. You can also obtain the bounding box information without drawing a string by using the ImageTTFBBox() function, which takes font size, angle, font filename, and text to be measured, and returns the same information as ImageTTFText() .



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net