Ubuntu Unleashed

16.3. Getting Arty

The imagefilledrectangle( ) function takes six parameters in total, which are, in order: an image resource to draw on, the top-left X coordinate, the top-left Y coordinate, the bottom-right X coordinate, the bottom-right Y coordinate, and a color to use. There is a similar function called imagerectangle( ), which takes the same parameters but only draws the outline of the rectangle, whereas imagefilledrectangle( ) fills the shape with color.

In order to draw a rectangle in such a way as to make it stand out, we need to allocate another color and then draw the rectangle. Here is how that is done:

$white = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 10, 10, 390, 290, $white);

Put those two lines just after the definition of $gold, then save the modified script and refresh phppicture.html.

This function becomes more interesting when used in a loop, like this:

$image = imagecreate(400,300); $gold = imagecolorallocate($image, 255, 240, 00); $white = imagecolorallocate($image, 255, 255, 255); $color = $white; for ($i = 400, $j = 300; $i > 0; $i -= 4, $j -= 3) { if ($color = = $white) { $color = $gold; } else { $color = $white; } imagefilledrectangle($image, 400 - $i, 300 - $j, $i, $j, $color); } imagepng($image); imagedestroy($image);

That script calls imagefilledrectangle( ) each iteration of the loop, slowly making the rectangle smaller and smaller as $i and $j decrease in value. Your output should look like Figure 16-2.

In place of a plain color, it is possible to fill your shapes with a tiled image using the imagesettile( ) function.

Figure 16-2. Using a simple loop, we've turned our simple rectangle into a series of concentric rectangles

Категории