Page 571 - Beginning PHP 5.3
P. 571

Chapter 17:  Generating Images with PHP


                         Next the script sets up a for loop to iterate through each of the five built-in system fonts:
                          for ( $i = 1; $i <= 5; $i++ ) {
                         Within the loop, the text is drawn using the system font with an index of $i. The text is positioned 5
                         pixels from the left edge of the image. The $yOffset variable positions it vertically in the image.

                             imagestring( $textImage, $i, 5, $yOffset, “This is system font $i”, $black );
                         Next, $yOffset is increased by the height of the current font to position the next line below the
                         current line. It uses the imagefontheight() function to return the height of a character in the font, in
                         pixels. (If you want to get the width of a character in a font, use the imagefontwidth() function.)

                          $yOffset += imagefontheight( $i );
                         After the loop, the script outputs the image and cleans up:

                          }

                          header( “Content-type: image/png” );
                          imagepng( $textImage );
                          imagedestroy( $textImage );
                          ?>





                           Using TrueType Fonts

                           When drawing basic charts and graphs you may prefer to use the built - in system fonts because they are
                         nonproportional fonts  —  all of the character widths are the same  —  so it makes layout and positioning
                         easier. However, if you ’ d like your text to look more elegant, you probably want to use a TrueType font.
                         These fonts offer a lot more versatility  —  not only can you control what your text looks like by choosing
                         from a wide range of available fonts, but you can also specify the size of the text and an angle at
                         which to draw it.

                           The preferred function to draw TrueType text is   imagefttext() , which uses the FreeType 2 library. The
                         function takes the following arguments, in order:

                            ❑       The image resource containing the image to write the text on
                            ❑       The font size in points
                            ❑       The angle at which to rotate the text, in degrees. Zero degrees is the three o ’ clock position,
                                90 degrees is the twelve o ’ clock position, and so on. A value of zero produces standard left - to -
                                  right text. (Notice that, unlike the corresponding   imagearc()  parameter, this angle works in a
                                counterclockwise direction rather than clockwise.)
                            ❑     The  x  and  y  position where you want the text to start. That ’ s the bottom - left corner of the
                                bounding box around the text. This is different from the   imagestring()  function, where the
                                coordinates represent the top - left corner of the bounding box


                                                                                                         533





                                                                                                      9/21/09   2:48:48 PM
          c17.indd   533
          c17.indd   533                                                                              9/21/09   2:48:48 PM
   566   567   568   569   570   571   572   573   574   575   576