Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.3     Getting arty: imagefilledrectangle()

This is NOT the latest copy of this book; click here for the latest version.

bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color)

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 colour to use. Note that there is a similar function called imagerectangle(), which takes exactly the same parameters with the difference being that it only draws the outline of the rectangle, whereas imagefilledrectangle() fills the shape in with colour.

In order to draw a rectangle in such a way as to make it stand out, we need to allocate another colour, 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. Not surprisingly, we now have a rectangle in there.

In case you are getting bored with simple stuff, let's take a leap forward with imagefilledrectangle() and draw a pattern using a loop. Take a copy of picture1.php and call it picture2.php - be sure to modify your HTML so that the image SRC points to picture2.php.

Bring up picture2.php in your favourite editor, and modify it to this:

<?php
    $image
= imagecreate(400,300);
    
$gold = imagecolorallocate($image, 255, 240, 00);
    
$white = imagecolorallocate($image, 255, 255, 255);
    
$colour = $white;

    for (
$i = 400, $j = 300; $i > 0; $i -= 4, $j -= 3) {
        if (
$colour == $white) {
            
$colour = $gold;
        } else {
            
$colour = $white;
        }

        
imagefilledrectangle($image, 400 - $i, 300 - $j, $i, $j, $colour);
    }

    
imagepng($image);
    
imagedestroy($image);
?>

The code is pretty much the same as before, with the addition of a simple for loop. Note that our loop sets the starting value for $i and $j, and also decrements $i and $j with each iteration.

As you can see in the code, we call imagefilledrectangle() each iteration of the loop, slowly making the rectangle smaller and smaller as $i and $j decrease in value. Save the script, and see how it looks in your web browser. If it is not the same as the screenshot below , check your code over and try again.



Author's Note: In place of a plain colour, it is possible to fill your shapes with a tiled image using the imagesettile() function.





<< 11.2.2 Choosing a format   11.2.4 More shapes: imagecreatetruecolor(), imagefilledellipse(), imagefilledarc(), imageellipse(), imagearc(), and imagerectangle() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
baljit0506@yahoo.co.in - 08 Sep 2008

this code not work , image not display in browser

Kyku - 08 Sep 2008

Isn't it that the ractangles are drawn no only to the center of the image, but afterwards towards its border. On the way outwards, you are removing what you have drawn in the move to centre. Effectively, the last iteration is with $i == 4 and $j = 3, and draws with white color. The solution is to change the loop condition. The $i > 200 is because of $i > 400 - $i.

A PHP User - 08 Sep 2008

Strange but true... I ran the same code with the same results.

A point of note: I have just returned to this page (I downloaded 26 Jun 2005) only to find today (28 Jul 2005) the same users comment with no solution!

That said this book is the best on the net for detailed information. Others lack the little details that a novice requires.

A PHP User - 08 Sep 2008

When I ran the code above, I got 1 gold rectangle totally filled in with white.

After experimenting, I discovered that you would get the result shown above by the author if you changed the for loop to read $i > 200 rather than $i > 0.

If I changed it to $i > 100 or $i > 300 I would get the exact same result which was a gold square in the middle of about 1/2 the total size.

Furthur if I used imagerectangle() instead of imagefilledrectangle(), I would also get the authors pattern if I coded $i>0, or $i>100, or $i>200.

I don't understand it, but thats what happened.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow