Page 556 - Beginning PHP 5.3
P. 556
Part III: Using PHP in Practice
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 );
}
// An example rectangle
$myImage = imagecreate( 200,100 );
$myGray = imagecolorallocate( $myImage, 204, 204, 204 );
$myBlack = imagecolorallocate( $myImage, 0, 0, 0 );
roundedRectangle( $myImage, 20, 10, 180, 90, 20, $myBlack );
header( “Content-type: image/png” );
imagepng( $myImage );
imagedestroy( $myImage );
?>
Figure 17-13 shows the script’s output.
Figure 17-13
How It Works
There’s nothing here that you haven’t seen in the last few pages; the only trick to creating a rounded
rectangle is in understanding how you combine the lines and arcs to get the effect that you want.
First the script creates a function to draw the rounded rectangle. By storing the code in a function you
can reuse it later if you want to draw other rounded rectangles with different dimensions. The
function has seven parameters:
function roundedRectangle( $image, $x1, $y1, $x2, $y2, $curveDepth, $color )
The first parameter is the resource of the image within which you want to draw the rectangle. The next
two parameters specify the top-left corner of the rectangle, and the next two parameters are for the
bottom-right corner of the rectangle. (The function doesn’t actually draw anything on these points
because it’s drawing a rectangle that has rounded corners, but they serve as anchor points for the corners
of the rectangle.) The sixth parameter, $curveDepth, is the number of pixels before the end of each side
518
9/21/09 2:48:40 PM
c17.indd 518 9/21/09 2:48:40 PM
c17.indd 518