Page 555 - Beginning PHP 5.3
P. 555

Chapter 17:  Generating Images with PHP


                           This example creates an array of the polygon ’ s points, called  $myPoints . There are six elements in the
                         array, or three  x/y  coordinate pairs. This means that there are three corners to this polygon: at   (20,20) ,
                           (185,55) , and  (70,80) .
                           The code then calls the   imagepolygon()  function, passing it the following parameters:

                               1.     The image resource.
                               2.       The array of points.
                               3.       The number of points in the polygon.

                               4.       The color with which to draw the shape.
                           Figure 17 - 12 shows the example polygon.











                                                  Figure 17-12

                               By the way, if you want to create filled shapes, you can use functions such as  imagefilledrectangle() ,
                               imagefilledellipse() ,  imagefilledarc() , and  imagefilledpolygon() . Find out more at
                               http://www.php.net/manual/en/ref.image.php .


                       Try It Out     Draw a Rectangle with Rounded Corners
                         Now that you know how to draw lines and arcs, you can create a handy function to draw rounded
                         rectangles. Save the following script as rounded_rectangle.php and open its URL in your browser:
                             <?php
                             function roundedRectangle( $image, $x1, $y1, $x2, $y2, $curveDepth, $color )
                             {
                               // Draw the four sides
                               imageline( $image, ($x1 + $curveDepth), $y1, ($x2 - $curveDepth), $y1,
                             $color );
                               imageline( $image, ($x1 + $curveDepth), $y2, ($x2 - $curveDepth), $y2,
                             $color );
                               imageline( $image, $x1, ($y1 + $curveDepth), $x1, ($y2 - $curveDepth),
                             $color );
                               imageline( $image, $x2, ($y1 + $curveDepth), $x2, ($y2 - $curveDepth),
                             $color );

                               // Draw the four corners
                               imagearc( $image, ($x1 + $curveDepth), ($y1 + $curveDepth), (2 *
                             $curveDepth), (2 * $curveDepth), 180, 270, $color );
                               imagearc( $image, ($x2 -$curveDepth), ($y1 + $curveDepth), (2 *
                             $curveDepth), (2 * $curveDepth), 270, 360, $color );



                                                                                                         517





                                                                                                      9/21/09   2:48:40 PM
          c17.indd   517
          c17.indd   517                                                                              9/21/09   2:48:40 PM
   550   551   552   553   554   555   556   557   558   559   560