18.4 Adding Text to Images


You want to add text to your images.

Technique

There are quite a few functions that enable you to write text to your images. The "Discussion" section contains a list of all the different functions. For information on drawing TrueType fonts to an image, see recipe 18.14.

Comments

The following sections comprise a list of the different functions that you can use to draw text to images with PHP's gd functions.

ImageChar
 int ImageChar(int im, int font, int x, int y, string c, int col); 

Draw a character horizontally.

Example
 <?php header("Content-type: image/jpeg"); $im = ImageCreate(140, 50); $str = "Hello"; $bgcol = ImageColorAllocate($im, 234, 122, 99); $txtcol = ImageColorAllocate($im, 0, 0, 0); for ($i=0, $x=10; $i < strlen($str); $i++, $x+=15) {     ImageChar($im, 5, $x, 20, $str[$i], $txtcol); } ImageJpg($im); ImageDestroy($im); ?> 
ImageCharUp
 int ImageCharUp(int im, int font, int x, int y, string c, int col); 

Draw a character vertically.

Example
 <?php header("Content-type: image/gif"); $str = "Hello"; $im = ImageCreate(50, 140); $bgcol = ImageColorAllocate($im, 234, 122, 99); $txtcol = ImageColorAllocate($im, 0, 0, 0); for ($i=0, $y=20; $i < strlen($str); $i++, $y+=15) {     ImageCharUp($im, 5, 10, $y, $str[$i], $txtcol); } ImageGif($im); ImageDestroy($im); ?> 
ImageLoadFont
 int ImageLoadFont(string file); 

Load a user -defined font.

Example
 <?php header("Content-type: image/png"); $im = ImageCreate(140, 50); $font = ImageLoadFont("myfont.ft"); $bgcolor = ImageColorAllocate($im, 222, 4, 232); $txt_color = ImageColorAllocate($im, 23, 3, 32); ImageString($im, $font, 20, 10, "Some Str", $txt_color); ImagePng($im); ImageDestroy($im); ?> 
ImagePsLoadFont
 int ImagePsLoadFont(string file); 

Load a PostScript 1 font.

Example
 <?php header("Content-type: image/png"); $im = ImageCreate(140, 50); $black = ImageColorAllocate($im, 0, 0, 0); $white = ImageColorAllocate($im, 255, 255, 255); $font = ImagePsLoadFont("postscript_font.ttf"); ImagePsText($im, "Hello", $font, 13, $white, $black, 20, 10); ImagePsFreeFont($font); ImagePng($im); ImageDestroy($im); ?> 
ImagePsText
 array imagepstext(int image, string text, int font, int size,                   int foreground, int background, int x, int y,                   int [space], int [tightness], float [angle],                   int [antialias_steps]); 

Draw text with a Postscript 1 type font.

Example

See example for ImagePsLoadFont() .

ImagePsFreeFont
 void ImagePsFreeFont(int fontindex); 

Free memory associated with a font loaded by ImagePsLoadFont() .

Example

See the example for ImagePsLoadFont() .

ImagePsEncodeFont
 int ImagePsEncodeFont(string encodingfile); 

Change the character encoding vector for a font, which is often useful when dealing with other languages that require characters above ASCII 127.

Example
 <?php header("Content-type: image/gif"); $im = ImageCreate(140, 50); $blue = ImageColorAllocate($im, 0, 0, 255); $white = ImageColorAllocate($im, 255, 255, 255); $font = ImagePsLoadFont("some_postscript_font.ps"); $font = ImagePsEncodeFont("IsoLatin1.enc"); ImagePsText($im, "Hello~", $font, 12, $white, $blue, 20, 10); ImagePsFreeFont($font); ImagePng($im); ImageDestroy($im); ?> 
ImageString
 int ImageString(int im, int font, int x, int y, string s, int col); 

Draw a string horizontally.

Example
 <?php header("Content-type: image/png"); $im = ImageCreate(140,50); $green = ImageColorAllocate($im, 0, 255, 0); $white = ImageColorAllocate($im, 255, 255, 255); ImageString($im, 4, 20, 10, "Hello World", $white); ImagePng($im); ImageDestroy($im); ?> 
ImageStringUp
 int ImageStringUp(int im, int font, int x, int y, string s, int col); 

Draw a string vertically.

Example
 <?php header("Content-type: image/jpeg"); $im = ImageCreate(50, 140); $blue = ImageColorAllocate($im, 0, 0, 255); $mix = ImageColorAllocate($im, 190, 34, 3); ImageStringUp($im, 4, 10, 70, "Hello", $mix); ImageJpeg($im); ImageDestroy($im); ?> 


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