Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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.

Tip

In place of a plain color, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page