imagerectangle() draws only the border. Use imagesetthickness() when a thicker outline is needed.
<?php
$image = imagecreatetruecolor(400, 240);
$background = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 200, 50, 50);
imagefilledrectangle($image, 0, 0, 399, 239, $background);
imagesetthickness($image, 3);
imagerectangle($image, 60, 50, 340, 190, $red);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>Current example first; the original tutorial examples below are retained and cleaned up for continued learning.
header ("Content-type: image/jpeg");
$im = @ImageCreate (200, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 128);
$text_color = ImageColorAllocate ($im, 255, 0, 0);
imagerectangle ($im,0,0,199,199,$text_color);
//imagerectangle ($im,0,0,200,200,$text_color);
Imagejpeg ($im);
imagedestroy($im); //memory is removed.
![]() | ![]() |
| imagerectangle ($im,0,0,199,199,$text_color); | imagerectangle ($im,0,0,200,200,$text_color); |
imagerectangle($image,$x1,$y1,$x2,$y2,$color)
$image | Image created using ImageCreate() or imagecreatetruecolor() |
$x1 | Upper left ( X : Horizontal ) coordinate of the rectangle |
$y1 | Upper left ( Y : Vertical ) coordinate of the rectangle |
$x2 | Bottom right ( X : Horizontal ) coordinate of the rectangle |
$y2 | Bottom right ( Y : Vertical ) coordinate of the rectangle |
$color | Color of the borders by using imagecolorallocate(). |

header ("Content-type: image/jpeg");
$im = @ImageCreate (200, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 128);
$text_color = ImageColorAllocate ($im, 255, 0, 0); // red
imagerectangle ($im,20,20,100,100,$text_color);
Imagejpeg ($im);
imagedestroy($im); //memory is removed.

header ("Content-type: image/jpeg");
$im = @ImageCreate (200, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 128);
$text_color = ImageColorAllocate ($im, 255, 0, 0); // red
imagerectangle ($im,20,20,100,100,$text_color);
$text_color = ImageColorAllocate ($im, 0, 0, 255); // blue
imagerectangle ($im,50,50,150,150,$text_color);
Imagejpeg ($im);
imagedestroy($im); //memory is removed.

header ("Content-type: image/jpeg");
$im = @ImageCreate (601, 601) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$gap=10; // change this value to increase the lines
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 128, 255, 136);
imagesetthickness($im, 2);// thickness is changed
for($i=0;$i<=600;$i +=$gap){
imagerectangle ($im,$i,$i,(600-$i),(600-$i),$text_color);
}
Imagejpeg ($im);
imagedestroy($im); //memory is removed.

header ("Content-type: image/jpeg");
$im = @ImageCreate (601, 601) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$gap=10; // change this value to increase the lines
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 128, 255, 136);
imagesetthickness($im, 2);// thickness is changed
for($i=0;$i<=600;$i +=$gap){
$r=rand(1,255);
$g=rand(1,255);
$b=rand(1,255);
$text_color = ImageColorAllocate ($im, $r, $g, $b);
imagerectangle ($im,$i,$i,(600-$i),(600-$i),$text_color);
}
Imagejpeg ($im);
imagedestroy($im); //memory is removed.
GD imagefilledrectangle() to create Bar Chart Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.