Page 572 - Beginning PHP 5.3
P. 572

Part III: Using PHP in Practice
                   ❑       The text color, as a color index (as returned by  imagecolorallocate() , for example)
                   ❑       The full path to the font (  .ttf ) file on your server ’ s hard disk
                   ❑       The string of text to draw

                    imagefttext()  draws the text, then returns an eight - element array representing the four corner points
                 of the bounding box enclosing the drawn text:



                 Element Index              Description
                 0                          The x-coordinate of the lower-left corner
                 1                          The y-coordinate of the lower-left corner

                 2                          The x-coordinate of the lower-right corner
                 3                          The y-coordinate of the lower-right corner
                 4                          The x-coordinate of the upper-right corner

                 5                          The y-coordinate of the upper-right corner
                 6                          The x-coordinate of the upper-left corner
                 7                          The y-coordinate of the upper-left corner



                  This means that you can find out exactly how much space the drawn text has taken up in your image. This
              is useful if you then want to draw more text or shapes that are positioned relative to the text you ’ ve
              just drawn.



              Try It Out     Draw Text with a TrueType Font
                Here’s an example script that displays some text using a TrueType font. Save the code as truetype.
                php in your document root folder and run the script in your Web browser. (If you don’t have the Vera
                font installed at /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf, change the script
                to point to a TrueType font that you do have installed.)

                    <?php
                    $textImage = imagecreate( 200, 120 );
                    $white = imagecolorallocate( $textImage, 255, 255, 255 );
                    $black = imagecolorallocate( $textImage, 0, 0, 0 );
                    imagefttext( $textImage, 16, 0, 10, 50, $black, “/usr/share/fonts/truetype/
                    ttf-bitstream-vera/Vera.ttf”, “Vera, 16 pixels” );
                    header( “Content-type: image/png” );
                    imagepng( $textImage );
                    imagedestroy( $textImage );
                    ?>

                Figure 17-24 shows the resulting text.


              534





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