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, ...
Get PHP in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.