[ Team LiB ] Previous Section Next Section

Drawing a Rectangle

You can draw a rectangle in PHP using the imagerectangle() function. imagerectangle() requires an image resource, the coordinates for your rectangle's upper-left corner, the coordinates for its bottom-right corner, and a color resource. The following fragment draws a rectangle whose upper-left coordinates are 19, 19 and bottom-right coordinates are 179, 179:


imagerectangle( $image, 19, 19, 179, 179, $blue );

You could then fill this with imagefill(). Because this is such a common operation, however, PHP provides the imagefilledrectangle() function, which expects exactly the same arguments as imagerectangle() but produces a rectangle filled with the color you specify. Listing 15.5 creates a filled rectangle (line 6) and outputs the image to the browser.

Listing 15.5 Drawing a Filled Rectangle with imagefilledrectangle()
1: <?php
2: header("Content-type: image/png");
3: $image = imagecreate( 200, 200 );
4: $red = imagecolorallocate($image, 255,0,0);
5: $blue = imagecolorallocate($image, 0,0,255 );
6: imagefilledrectangle( $image, 19, 19, 179, 179, $blue );
7: imagepng( $image );
8: ?>

Figure 15.5 shows the output from Listing 15.5.

Figure 15.5. Drawing a filled rectangle with imagefilled rectangle().

graphics/15fig05.gif

    [ Team LiB ] Previous Section Next Section