Page 551 - Beginning PHP 5.3
P. 551

Chapter 17:  Generating Images with PHP

                                                                        60

                                                                        x   120
                                                            120         y   60
                                                      Figure 17-4
                           Drawing Lines
                           To draw a line in an image use the  imageline()  function. A line has both start and end points, so you
                          must give   imageline()  two sets of coordinates. For example:

                             imageline( $myImage, 10, 10, 100, 100, $myColor );




                       Try It Out     Drawing a Line
                         In this simple example you draw a straight line between two points, then output the resulting image
                         to the browser. Save the following code as line.php in your document root folder:
                             <?php
                             $myImage = imagecreate( 200, 100 );
                             $myGray = imagecolorallocate( $myImage, 204, 204, 204 );
                             $myBlack = imagecolorallocate( $myImage, 0, 0, 0 );
                             imageline( $myImage, 15, 35, 120, 60, $myBlack );
                             header( “Content-type: image/png” );
                             imagepng( $myImage );
                             imagedestroy( $myImage );
                             ?>

                         Now run the script by opening its URL in your Web browser. Figure 17-5 shows the output.







                                                       Figure 17-5
                             If you’re running PHP on Windows and you receive a Call to undefined function
                             imagecreate() error message, you need to enable the GD2 extension. To do this, edit your php.ini
                             file and remove the semicolon from the start of the following line:
                             ;extension=php_gd2.dll
                             Remember to restart your server after you’ve made this change.
                             These steps are normally unnecessary on other systems such as Linux and Mac OS X, because the GD2
                             library is usually bundled with the PHP engine on these systems. For more information see
                             http://www.php.net/manual/en/image.installation.php.

                         How It Works
                         The script first creates a new blank image with imagecreate()and stores the image resource in
                         $myImage. Then it allocates two colors — a gray and a black. Because the gray is allocated first, it is
                         used as the background color of the image.
                                                                                                         513





                                                                                                      9/21/09   2:48:37 PM
          c17.indd   513
          c17.indd   513                                                                              9/21/09   2:48:37 PM
   546   547   548   549   550   551   552   553   554   555   556