Page 569 - Beginning PHP 5.3
P. 569

Chapter 17:  Generating Images with PHP


















                                 Figure 17-22


                           Using Text in Images

                           Adding text to images with PHP enables you to annotate images or draw dynamic charts and graphs.
                         The quickest and easiest way to add text to an image is to use the   imagestring()  function, which lets
                          you draw a string of text on your image at the location you specify.

                           Adding Standard Text

                           It ’ s easy to draw on your image with  imagestring()  because the function can use the set of built - in
                          system fonts for your text. This means that you don ’ t have to worry about loading a specific font on
                          your server.
                             imagestring()  takes six parameters:

                            ❑     The image resource
                            ❑       The font to use for the text. This is an integer value and ranges from   1  upward. Values between
                                  1  and  5  are the built - in system fonts; any fonts you load subsequently are represented by
                                numbers starting from 6
                            ❑     The  x  and  y  values of the position of the text. These represent the top - left corner of the rectangle
                                within which your text will appear
                            ❑       The text string that you want to draw
                            ❑       The color of the text

                               To load a font for the   imagestring()  function, use the  imageloadfont()  function. It loads a
                             bitmap font that is architecture dependent; this means the font needs to be generated on the same type of
                             system that you want to use it on. A far easier solution is to use TrueType fonts  —  which you get to in
                             just a bit.

                             imagestring()  returns  true  if it successfully added the text to the image, or  false  if there
                          was an error.



                                                                                                         531





                                                                                                      9/21/09   2:48:47 PM
          c17.indd   531
          c17.indd   531                                                                              9/21/09   2:48:47 PM
   564   565   566   567   568   569   570   571   572   573   574