Page 558 - Beginning PHP 5.3
P. 558

Part III: Using PHP in Practice
                 Because this is the top-left corner, the arc starts at 180 degrees (the 9 o’clock position) and curves around
                 to 270 degrees (the 12 o’clock position):

                      imagearc( $image, ($x1 + $curveDepth), ($y1 + $curveDepth), (2 *
                    $curveDepth), (2 * $curveDepth), 180, 270, $color );

                 The rest of the corners are created in exactly the same way, except that the function either adds or
                 subtracts $curveDepth from $x1, $y1, $x2, and $y2 as appropriate to get the correct center points for
                 each arc. Also remember that the start and end positions of the arcs change for each corner.

                      imagearc( $image, ($x2 - $curveDepth), ($y1 + $curveDepth), (2 *
                    $curveDepth), (2 * $curveDepth), 270, 360, $color );
                      imagearc( $image, ($x2 - $curveDepth), ($y2 - $curveDepth), (2 *
                    $curveDepth), (2 * $curveDepth), 0, 90, $color );
                      imagearc( $image, ($x1 + $curveDepth), ($y2 - $curveDepth), (2 *
                    $curveDepth), (2 * $curveDepth), 90, 180, $color );
                    }

                 That’s the end of the rounded rectangle function, so now the script can draw the image.
                 First the script creates the blank image and allocates two colors to the image. The first color allocated
                 (gray) is the background color for the image, and the second color (black) is used for the rectangle:

                    $myImage = imagecreate( 200, 100 );
                    $myGray = imagecolorallocate( $myImage, 204, 204, 204 );
                    $myBlack = imagecolorallocate( $myImage, 0, 0, 0 );

                 Next the code calls the roundedRectangle() function, passing it the arguments previously discussed:

                    roundedRectangle( $myImage, 20, 10, 180, 90, 20, $myBlack );
                 Finally, the rectangle image is sent to the Web browser by calling header() followed by imagepng(). To
                 finish up, the imagedestroy() function is called to clean up memory:

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





                  Manipulating Images

                   So far in this chapter you ’ ve seen how to create images using the drawing functions of the GD image
                 library, and you ’ ve created some basic shapes. But what happens if you want to work with existing
                 images? Well, PHP doesn ’ t restrict you to creating new images  —  you can just as easily produce a
                 new image that is based on an existing JPEG, PNG, or GIF image.




              520





                                                                                                      9/21/09   2:48:41 PM
          c17.indd   520                                                                              9/21/09   2:48:41 PM
          c17.indd   520
   553   554   555   556   557   558   559   560   561   562   563