imagerectangle(): Rectangles using PHP GD

imagerectangle($image,$x1,$y1,$x2,$y2,$color)
$imageImage created using ImageCreate() or imagecreatetruecolor()
$x1Upper left ( X : Horizontal ) coordinate of the rectangle
$y1Upper left ( Y : Vertical ) coordinate of the rectangle
$x2Bottom right ( X : Horizontal ) coordinate of the rectangle
$y2Bottom right ( Y : Vertical ) coordinate of the rectangle
$colorColor of the borders by using imagecolorallocate().

Drawing rectangles & patterns using different thickness and colours using imagerectangle() in PHP GD


Note that the canvas starts from 0,0 position. So to add border around one image we have to leave one pixel from the right and bottom edges.
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.
with right and bottom borderwithout right and bottom border
imagerectangle ($im,0,0,199,199,$text_color);imagerectangle ($im,0,0,200,200,$text_color);

Example 1

Draw one rectangle using red colour.
Rectangle in red
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. 

Using two rectangles by changing colour

Two Rectangles in red and blue
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.

Thickness of lines and generating patterns

By changing the value of $gap the gap between the rectangles can be changed. We used imagesetthickness() to change the thickness of the rectangles. Patterns of rectangles
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. 

Changing colour of patterns

Colour Patterns of rectangles
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 functions GD imagearc()

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    GD functions in PHP for handling images & graphics

    How to check GD support in PHP
    Header for an image in browser
    Drawing of graphic lines using GD
    Plotting line graph by drawing data from table
    Adding vertical grid to line graph
    Adding rectangles using imagerectangle()
    Imagearc drawing arc and circle
    Imagefilledarc drawing filled arc and circle
    Resizing and adding border to Images
    Script for Watermarking image on the fly using PHP GD
    Generate image by using user input form data

    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer