Page 552 - Beginning PHP 5.3
P. 552

Part III: Using PHP in Practice
                Next the script calls the imageline() function to draw the line. The first argument is the image
                resource. The next two arguments tell the imageline() function where the line starts — in this
                example, 15 pixels across and 35 pixels down from the top-left corner. The next two parameters then
                tell the function where the line should end — 120 pixels across and 60 pixels down in the example.
                The last parameter is, of course, the color in which to draw the line.

                After the image is drawn, the script calls the header() and imagepng() functions to output the
                image to the Web browser. Finally, the image is removed from memory by calling the
                imagedestroy() function.




                  Drawing Rectangles
                   To draw a rectangle you only need to specify two positions on the canvas: the two opposite corners of
                 your rectangle. Because of this, the syntax for the   imagerectangle()  function is exactly the same as the
                   imageline()  function. In the case of  imagerectangle() , the two coordinates you provide are used as
                opposite corners of the rectangle.
                  To try this out, open the   line.php  file that you just created and save it as  rectangle.php . Replace
                the line:

                    imageline( $myImage, 15, 35, 120, 60, $myBlack );
                 with the line:

                    imagerectangle( $myImage, 15, 35, 120, 60, $myBlack );

                   As you can see, the arguments passed to the  imagerectangle()  function are exactly the same as those
                 used in the line - drawing example. Save the file as   rectangle.php  and open the script ’ s URL in your
                 Web browser. Figure 17 - 6 shows the image generated by this code.










                                                Figure 17-6
                   If you left the  imageline()  call in the code as well, you ’ d get the output shown in Figure 17 - 7.












                                             Figure 17-7
              514





                                                                                                      9/21/09   2:48:38 PM
          c17.indd   514
          c17.indd   514                                                                              9/21/09   2:48:38 PM
   547   548   549   550   551   552   553   554   555   556   557