TechniqueThere 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. CommentsThe following sections comprise a list of the different functions that you can use to draw text to images with PHP's gd functions. ImageCharint 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); ?> ImageCharUpint 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); ?> ImageLoadFontint 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); ?> ImagePsLoadFontint 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); ?> ImagePsTextarray 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. ExampleSee example for ImagePsLoadFont() . ImagePsFreeFontvoid ImagePsFreeFont(int fontindex); Free memory associated with a font loaded by ImagePsLoadFont() . ExampleSee the example for ImagePsLoadFont() . ImagePsEncodeFontint 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); ?> ImageStringint 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); ?> ImageStringUpint 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); ?> |