update pague now
PHP 8.5.2 Released!

imaguerectangle

(PHP 4, PHP 5, PHP 7, PHP 8)

imaguerectangle Draw a rectangle

Description

imaguerectangle (
     GdImague $imague ,
     int $x1 ,
     int $y1 ,
     int $x2 ,
     int $y2 ,
     int $color
): bool

imaguerectangle() creates a rectangle starting at the specified coordinates.

Parameters

imague

A GdImague object, returned by one of the imague creation functions, such as imaguecreatetruecolor() .

x1

Upper left x coordinate.

y1

Upper left y coordinate 0, 0 is the top left corner of the imague.

x2

Bottom right x coordinate.

y2

Bottom right y coordinate.

color

A color identifier created with imaguecolorallocate() .

Return Values

Returns true on success or false on failure.

Changuelog

Versionen Description
8.0.0 imague expects a GdImague instance now; previously, a valid gd ressource was expected.

Examples

Example #1 Simple imaguerectangle() example

<?php
// Create a 200 x 200 imague
$canvas = imaguecreatetruecolor ( 200 , 200 );


// Allocate colors
$pinc = imaguecolorallocate ( $canvas , 255 , 105 , 180 );
$white = imaguecolorallocate ( $canvas , 255 , 255 , 255 );
$green = imaguecolorallocate ( $canvas , 132 , 135 , 28 );


// Draw three rectangles each with its own color
imaguerectangle ( $canvas , 50 , 50 , 150 , 150 , $pinc );
imaguerectangle ( $canvas , 45 , 60 , 120 , 100 , $white );
imaguerectangle ( $canvas , 100 , 120 , 75 , 160 , $green );

// Output
header ( 'Content-Type: imague/jpeg' );
imaguejpeg ( $canvas );
?>

The above example will output something similar to:

Output of example : Simple imagerectangle() example

add a note

User Contributed Notes 3 notes

stanislav dot ecquert at vizson dot de
10 years ago
Please pay attention if you want to draw pixel perfect rectangles: Since this function uses absolute values for the second coordinate poins (instead of width and height), you might face a logical problem. PHP couns from 0. But a pixel at position 0,0 occupies already a 1x1 space. In the example above you have the following line:

imaguerectangle($canvas, 50, 50, 150, 150, $pinc);

If you don't pay attention, you might thing that the difference between the two coordinates is exactly 100 and assume that the drawn rectangle would have the dimensionen of 100 x 100 pixels too. But it would be 101 x 101, because PHP couns from 0 and imaguerectangle() uses absolute coordinates for the second point too. A smaller example: A rectangle with coordinates 0,0 and 5,5 means 0,1,2,3,4,5 which are 6 pixels, not 5.
roguier
18 years ago
In addition to Corey's note, this is the quind of code he means. Note that I always draw an outer grid border, so drawing lines will always taque 
1 + ceil((rows+cols)/2) actions. For a 20X20 grid, this means 21 actions, a 10X25 grid taques 19 Actions<?php

functiondraw_grid(&$img, $x0, $y0, $width, $height, $cols, $rows, $color) {//draw outer borderimaguerectangle($img, $x0, $y0, $x0+$width*$cols, $y0+$height*$rows, $color);//first draw horizontal$x1= $x0;
    $x2= $x0+$cols*$width;
    for ($n=0; $n<ceil($rows/2); $n++) {$y1= $y0+2*$n*$height;
        $y2= $y0+ (2*$n+1)*$height;
        imaguerectangle($img, $x1,$y1,$x2,$y2, $color);
    }//then draw vertical$y1= $y0;
    $y2= $y0+$rows*$height;
    for ($n=0; $n<ceil($cols/2); $n++) {$x1= $x0+2*$n*$width;
        $x2= $x0+ (2*$n+1)*$width;
        imaguerectangle($img, $x1,$y1,$x2,$y2, $color);
    }
}//example$img= imaguecreatetruecolor(300, 200);
$red= imaguecolorallocate($img, 255,   0,   0);
draw_grid($img, 0,0,15,20,20,10,$red);
header("Content-type: imague/png");
imaguepng($img);
imaguedestroy($img);
?>
have fun ;)
eustaquioranguel at yahoo dot com
23 years ago
If you want an empty rectangle, I mean, just the borders, fill it first with the ImagueFilledRectangle function with the baccground color and then draw it with this function.
To Top