# imagefilledrectangle

(PHP 4, PHP 5, PHP 7)

imagefilledrectangleDraw a filled rectangle

### 설명

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

Creates a rectangle filled with `color` in the given `image` starting at point 1 and ending at point 2. 0, 0 is the top left corner of the image.

### 인수

``` image```

imagecreatetruecolor() 등의 이미지 생성 함수에서 반환한 이미지 자원.

`x1`

x-coordinate for point 1.

`y1`

y-coordinate for point 1.

`x2`

x-coordinate for point 2.

`y2`

y-coordinate for point 2.

`color`

The fill color. A color identifier created with imagecolorallocate().

### 반환값

성공 시 `TRUE`를, 실패 시 `FALSE`를 반환합니다.

### 예제

Example #1 imagefilledrectangle() usage

``` <?php// Create a 55x30 image\$im = imagecreatetruecolor(55, 30);\$white = imagecolorallocate(\$im, 255, 255, 255);// Draw a white rectangleimagefilledrectangle(\$im, 4, 4, 50, 25, \$white);// Save the imageimagepng(\$im, './imagefilledrectangle.png');imagedestroy(\$im);?> ```

위 예제의 출력 예시:

### User Contributed Notes 14 notes

michal dot kocarek at seznam dot cz
18 years ago
``` If you want to draw a rectangle with rounded corners, you can use this simple function... Rectangle starts at x1y1 and ends at x2y2. \$radius defines radius of circled corner. <?function ImageRectangleWithRoundedCorners(&\$im, \$x1, \$y1, \$x2, \$y2, \$radius, \$color) {// draw rectangle without cornersimagefilledrectangle(\$im, \$x1+\$radius, \$y1, \$x2-\$radius, \$y2, \$color);imagefilledrectangle(\$im, \$x1, \$y1+\$radius, \$x2, \$y2-\$radius, \$color);// draw circled cornersimagefilledellipse(\$im, \$x1+\$radius, \$y1+\$radius, \$radius*2, \$radius*2, \$color);imagefilledellipse(\$im, \$x2-\$radius, \$y1+\$radius, \$radius*2, \$radius*2, \$color);imagefilledellipse(\$im, \$x1+\$radius, \$y2-\$radius, \$radius*2, \$radius*2, \$color);imagefilledellipse(\$im, \$x2-\$radius, \$y2-\$radius, \$radius*2, \$radius*2, \$color);}?> ```
olivier dot pons at google dot mail dot com
10 years ago
``` I wanted to clear an image, and set it to full transparent. imagefilledrectangle() seems to ignore alpha channel and alpha blending. Use imagefill() instead: <?php \$w = imagesx(\$final); \$h = imagesy(\$final); \$grande = imagecreatetruecolor(\$w, \$h); // Alpha blending on to use channel alpha imagealphablending(\$grande, true); // Allocate a transparent color and fill the new image with it. // Without this the image will have a black background instead // of being transparent. \$transparent = imagecolorallocatealpha(\$grande, 0, 0, 0, 127); // transparent alpha will be _ignored_: imagefilledrectangle(\$grande, 0, 0, \$w, \$h, \$transparent); // ok, transparent will be used and set whole alpha channel to transparent: imagefill(\$grande, 0, 0, \$transparent); ?> ```
16 years ago
``` <?php//index.php //set your year, month, daym hour, minute, second you want to cuuntdown to.//ONLY CHANGE FROM HERE\$year="2006";\$month="12";\$day="25";\$hour="00";\$minute="00";\$second="00";\$event="Christmas Day 2006";\$time=mktime(\$hour, \$minute, \$second, \$month, \$day, \$year);\$timecurrent=date('U');\$cdtime=\$time-\$timecurrent;\$cdmonths=\$cddays/30;\$cdyears=\$cddays/365;//Used this case only...\$cdminutes=round(\$cdtime/60);//cdtime is seconds\$cdhours=round(\$cdtime/3600);\$cddays=round(\$cdhours/24);//String the date\$currentdate = date('l, F j, Y');// Set the content-typeheader("Content-type: image/png");// Create the image\$im = imagecreatetruecolor(701, 355);//Temp BGCOLOR (center of c-finder)\$bg1 = 208;\$bg2 = 130;\$bg3 = 208;\$s1 = \$bg1 - 20;\$s2 = \$bg2 - 20;\$s3 = \$bg3 - 20;\$t1 = \$bg1 + 30;\$t2 = \$bg2 + 30;\$t3 = \$bg3 + 30;\$cArray=array();    \$c1 = imagecolorallocate(\$im, \$bg1, \$bg2, \$bg3); //Background    \$c8 = imagecolorallocate(\$im, 255, \$bg2, 255); //Background    \$c2 = imagecolorallocate(\$im, \$s1, \$s2, \$s3); //Shadow    \$c3 = imagecolorallocate(\$im, \$t1, \$t2, \$t3); //Textimagefilledrectangle(\$im, 0, 0, 701, 50, \$c1);imagefilledrectangle(\$im, 0, 0, 701, 50, \$c8);// The text to draw\$text = \$string;// Replace path by your own font path\$fnum = rand(1, 9);\$font = "/f/font (\$fnum)";// Add some shadow to the textimagettftext(\$im, 29, 1, 17, 42, \$c2, \$font, "Today is:");imagettftext(\$im, 28, -1, 15, 40, \$c3, \$font, "Today is:");imagettftext(\$im, 29, 1, 17, 92, \$c2, \$font, "...\$currentdate...");imagettftext(\$im, 28, -1, 15, 90, \$c3, \$font, "...\$currentdate...");imagettftext(\$im, 29, 1, 17, 142, \$c2, \$font, "So there are exactly:");imagettftext(\$im, 28, -1, 15, 140, \$c3, \$font, "So there are exactly:");imagettftext(\$im, 29, 1, 17, 192, \$c2, \$font, "\$cddays with just...");imagettftext(\$im, 28, -1, 15, 190, \$c3, \$font, "\$cddays days with just...");imagettftext(\$im, 29, 1, 17, 242, \$c2, \$font, "\$cdminutes minutes and only...");imagettftext(\$im, 28, -1, 15, 240, \$c3, \$font, "\$cdminutes minutes and only...");imagettftext(\$im, 29, 1, 17, 292, \$c2, \$font, "\$cdseconds seconds until...");imagettftext(\$im, 28, -1, 15, 290, \$c3, \$font, "\$cdseconds seconds until...");imagettftext(\$im, 29, 1, 17, 342, \$c2, \$font, "- + \$event + -");imagettftext(\$im, 28, -1, 15, 340, \$c3, \$font, "- + \$event + -");// Using imagepng() results in clearer text compared with imagejpeg()imagepng(\$im);imagedestroy(\$im);?> ```
16 years ago
``` I've made a function to make full color gradients:<?php// The image must be in truecolor mode!!    function gradient_region(\$img, \$x, \$y, \$width, \$height,\$src_color, \$dest_color=0){        \$src_alpha = (\$src_color) >> 24;        \$src_red = (\$src_color & 0xFF0000) >> 16;        \$src_green = (\$src_color & 0x00FF00) >> 8;        \$src_blue = (\$src_color & 0x0000FF);                \$dest_alpha = (\$dest_color) >> 24;        \$dest_red = (\$dest_color & 0xFF0000) >> 16;        \$dest_green = (\$dest_color & 0x00FF00) >> 8;        \$dest_blue = (\$dest_color & 0x0000FF);                                \$inc_alpha = (\$dest_alpha - \$src_alpha) / \$width;        \$inc_red = (\$dest_red - \$src_red)/\$width;        \$inc_green = (\$dest_green - \$src_green)/\$width;        \$inc_blue = (\$dest_blue - \$src_blue)/\$width;                // If you need more performance, the step can be increased        for (\$i=0;\$i<\$width;\$i++){            \$src_alpha += \$inc_alpha;            \$src_blue += \$inc_blue;            \$src_green += \$inc_green;            \$src_red += \$inc_red;            imagefilledrectangle(\$img,                \$x+\$i,\$y,                         \$x+\$i,\$y+\$height,                imagecolorallocatealpha(\$img,                \$src_red,\$src_green,\$src_blue,\$src_alpha));        }    }?>More functions at http://www.sphoera.com ```
saramg at uclink dot berkeley dot edu
21 years ago
``` Important quirk to note: While imagerectangle will allow you to use a different order of your coordinates (such as bottom-left to upper-right), imagefilledrectangle will only work correctly if you use top-left to bottom-right as indicated in the docs. ```
-1
Darren Edale
9 years ago
``` The issue with filling using a rectangle is caused in your code by having alpha blending turned on before rendering the filled rectangle. Alpha blending causes what you draw on the image to be blended with whatever is already on the image according to the alpha channels of each. Therefore, because blending is on, and because the rectangle's fill colour is completely transparent, the blending of the existing image content with the transparent rectangle results in no change to the existing image.With blending off, when drawing to the image what you draw completely replaces what is already there. So, drawing the rectangle in this case results in the original content of the image being completely replaced with a transparent rectangle.So in order to use imagefilledrectangle() to erase an image to transparency, you need to turn off alpha blending first.I guess the reason imagefill() works with alpha blending on is because it does not perform any alpha blending - it always works without alpha blending regardless of the setting. I suspect there are reasons for this to do with alpha channels complicating edge detection.I would recommend using imagefilledrectangle() to create a blank transparent image resource instead of imagefill() because it is undoubtedly much faster in probably all cases.Here is some example code to blank an image to transparent, assuming \$im is a successfully created image:<?php\$transparent = imagecolorallocatealpha(\$im, 0, 0, 0, 127);imagealphablending(\$im, false);imagefilledrectangle(\$im, 0, 0, imagesx(\$im) - 1, \$imagesy(\$im) - 1, \$transparent);imagecolordeallocate(\$transparent);imagealphablending(\$im, true);?> ```
-1
mike at delenk dot net
14 years ago
``` The examples in function imagettfbbox() gave me many problems because of misunderstand of how the text is positionned in the box.So I did a new example:- writing a text- in the correct position in its box- with a padding aroundEnjoy !Mike<?// some settings    \$angle = 0;    \$text = 'Ühg 0123456789';    \$font_face = dirname(__FILE__).'/verdana.ttf'; // put the file in the same directory    \$font_size = 9; //(int) pixels in GD 1, or points in GD 2// retrieves box frame    \$box = imagettfbbox(\$font_size, 0, \$font_face, \$text);    \$bottom_left_x = \$box[0]; // used below    \$bottom_left_y = \$box[1]; // used below    \$bottom_right_x = \$box[2]; // used below    \$bottom_right_y = \$box[3];    \$top_right_x = \$box[4];    \$top_right_y = \$box[5];    \$top_left_x = \$box[6];    \$top_left_y = \$box[7]; // used below// define width and height of the text box    \$box_w = abs(\$bottom_left_x) + abs(\$bottom_right_x);    \$box_h = abs(\$bottom_left_y) + abs(\$top_left_y);// add padding    \$padding_x = 5;    \$padding_y = 5;    \$box_w = \$box_w + 2 * \$padding_x;    \$box_h = \$box_h + 2 * \$padding_y;// origin of the text = baseline of the first char    \$text_x = abs(\$bottom_left_x) -1 + \$padding_x;    \$text_y = \$box_h -1 - abs(\$bottom_left_y) - \$padding_y;    // mysterious correction for the characters going low like pgjq    if(abs(\$bottom_left_y) <= 1) \$box_h--;// create the image    \$img = imagecreatetruecolor(\$box_w, \$box_h); // define some colors    \$white = imagecolorallocate(\$img,255,255,255);    \$black = imagecolorallocate(\$img,0,0,0);    \$lightgrey = imagecolorallocate(\$img, 200, 200, 200);    \$grey = imagecolorallocate(\$img,100,100,100);    \$yellow = imagecolorallocate(\$img, 0xFF, 0xFF, 0x00); // attribute colors    \$font_color =  \$black;    \$padding_color =  \$lightgrey;    \$background_color =  \$yellow; // fill image with background color    imagefill(\$img, 0, 0, \$background_color);// fill image with padding color    imagefilledrectangle(\$img, 0, 0, \$box_w, \$padding_y - 1, \$padding_color); // top    imagefilledrectangle(\$img, 0, 0, \$padding_x - 1, \$box_h - 1, \$padding_color); // left    imagefilledrectangle(\$img, 0, \$box_h - \$padding_y -1, \$box_w, \$box_h - 1, \$padding_color); // bottom    imagefilledrectangle(\$img, \$box_w - \$padding_x, 0, \$box_w - 1, \$box_h - 1, \$padding_color); // right//write textimagettftext(\$img, \$font_size, 0, \$text_x, \$text_y, \$font_color, \$font_face, \$text);//rotate image    if (\$angle > 0) \$img = imagerotate(\$img, \$angle, \$white);// send header    header("Content-type: image/gif"); //sends image    imagegif(\$img);    imagedestroy(\$img);?> ```
-1
booga
19 years ago
``` a simple way of using imagerectangle to create a percentage bar<?php//this needs to reside in its own php page//you can include that php page in your html as you would an image://<IMG SRC="ratingpng.php?rating=25.2" border="0">function drawRating(\$rating) {    \$image = imagecreate(102,10);    \$back = ImageColorAllocate(\$image,255,255,255);    \$border = ImageColorAllocate(\$image,0,0,0);    \$red = ImageColorAllocate(\$image,255,60,75);    \$fill = ImageColorAllocate(\$image,44,81,150);    ImageFilledRectangle(\$image,0,0,101,9,\$back);    ImageFilledRectangle(\$image,1,1,\$rating,9,\$fill);    ImageRectangle(\$image,0,0,101,9,\$border);    imagePNG(\$image);    imagedestroy(\$image);}Header("Content-type: image/png");drawRating(\$rating);?> ```
-2
terereese at hotmail dot com
16 years ago
``` I would like to inform developers about a problem I encountered when trying to use imagefilledrectangle.I noted that the order in which the start and end y coordinates are listed is extremely important.as in the statements below.if(\$this->d_values[\$i]['unit_value'] < 0)imagefilledrectangle(\$this->img,\$position_x, \$start_y , \$end_x, \$end_y ,\$d_color);            else                imagefilledrectangle(\$this->img, \$position_x,\$ end_y , \$end_x, \$start_y,\$d_colour);                        Thanks ```
-1
info at 555webdesign dot com
16 years ago
``` Thanks terereese. it took me over two hours to figure that one out.it worked locally: imagefilledrectangle(imagresource, int x1, int x2, int y1, inty2, color)BUT remote on my provider only this worked: imagefilledrectangle(imagresource, int x1, int y2, int x1, inty1, color)Any ideas why and where? ```
-1
me [a.t.] forestfactory [d.o.t.] de
17 years ago
``` As of PHP 5, it seems to be no longer necessary to draw the rectangle from the upper left to the lower right corner. This led me into big trouble porting a script developed under PHP 5 to PHP 4. ```
-2
Mice32
13 years ago
``` That script draws Serpinski's carpet: <?php     set_time_limit(5);     \$i = 4;        // Iterations     \$xy = 500;    // Picture size     \$img = imagecreatetruecolor(\$xy, \$xy);     \$white = imagecolorallocate(\$img, 255, 255, 255);     drawCarpet(0, 0, \$xy, \$xy, \$i);     function drawCarpet(\$a, \$b, \$c, \$d, \$n) {         global \$img, \$white;         if(\$n <= 0) return;         \$a1 = 2 * \$a / 3 + \$c / 3;         \$c1 = \$a / 3 + 2 * \$c / 3;         \$b1 = 2 * \$b / 3 + \$d / 3;         \$d1 = \$b / 3 + 2 * \$d / 3;         imagefilledrectangle(\$img, \$a1, \$b1, \$c1, \$d1, \$white);         drawCarpet(\$a, \$b, \$a1, \$b1, \$n - 1);         drawCarpet(\$a1, \$b, \$c1, \$b1, \$n - 1);         drawCarpet(\$c1, \$b, \$c, \$b1, \$n - 1);         drawCarpet(\$c1, \$b, \$c, \$b1, \$n - 1);         drawCarpet(\$a, \$b1, \$a1, \$d1, \$n - 1);         drawCarpet(\$c1, \$b1, \$c, \$d1, \$n - 1);         drawCarpet(\$a, \$d1, \$a1, \$d, \$n - 1);         drawCarpet(\$a1, \$d1, \$c1, \$d, \$n - 1);         drawCarpet(\$c1, \$d1, \$c, \$d, \$n - 1);     }     header('Content-Type: image/png');     imagepng(\$img); ?> ```
-3
Tobi
12 years ago
``` It looks like there are waves beetween the points, but it's straight.<?php\$maxwert = 300;\$size = 10;\$img = imagecreatetruecolor(\$maxwert, \$maxwert);imagecolorallocate(\$img, 0, 0, 0);   for(\$y=0;\$y<\$maxwert;\$y += \$size){     for(\$x=0;\$x<\$maxwert;\$x+=\$size){      \$r = rand(0,255);      \$g = rand(0,255);      \$b = rand(0,255);      \$color = imagecolorallocate(\$img, \$r, \$g, \$b);            imagefilledrectangle (\$img, \$x, \$y, \$x+\$size ,\$y+\$size, \$color);          }   }                  header("Content-type: image/png"); imagepng(\$img);?> ```
-3
ivank at 2xtreme dot net
21 years ago
``` As stated above, it needs to go from the top left corner to the bottom right corner. Just use this to flip it if neccessary:// flip them if neccessary (x3, y3 are temp vars)if(\$x1 > \$x2) { \$x3 = \$x2; \$x2 = \$x1; \$x1 = \$x3; }if(\$y1 > \$y2) { \$y3 = \$y2; \$y2 = \$y1; \$y1 = \$y3; }ImageFilledRectangle(\$im, \$x1, \$y1, \$x2, \$y2, \$color); ```