imagefilledrectangle

(PHP 4, PHP 5)

imagefilledrectangle — Draw 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

An image resource, returned by one of the image creation functions, such as 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 в случае возникновения ошибки.

Коментарии

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.
2001-11-29 21:42:37
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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);
2002-03-30 18:34:48
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
Автор:
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);

?>
2004-01-17 19:48:05
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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 corners
imagefilledrectangle($im$x1+$radius$y1$x2-$radius$y2$color);
imagefilledrectangle($im$x1$y1+$radius$x2$y2-$radius$color);
// draw circled corners
imagefilledellipse($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);
}

?>
2004-05-30 16:18:38
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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.
2005-10-12 19:44:38
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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
2006-05-03 06:33:11
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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
2006-07-11 23:02:53
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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?
2006-10-25 13:13:18
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
Автор:
<?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-type
header("Content-type: image/png");

// Create the image
$im imagecreatetruecolor(701355);

//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($im255$bg2255); //Background
   
$c2 imagecolorallocate($im$s1$s2$s3); //Shadow
   
$c3 imagecolorallocate($im$t1$t2$t3); //Text

imagefilledrectangle($im0070150$c1);

imagefilledrectangle($im0070150$c8);

// The text to draw
$text $string;
// Replace path by your own font path
$fnum rand(19);
$font "/f/font ($fnum)";

// Add some shadow to the text
imagettftext($im2911742$c2$font"Today is:");
imagettftext($im28, -11540$c3$font"Today is:");
imagettftext($im2911792$c2$font"...$currentdate...");
imagettftext($im28, -11590$c3$font"...$currentdate...");
imagettftext($im29117142$c2$font"So there are exactly:");
imagettftext($im28, -115140$c3$font"So there are exactly:");
imagettftext($im29117192$c2$font"$cddays with just...");
imagettftext($im28, -115190$c3$font"$cddays days with just...");
imagettftext($im29117242$c2$font"$cdminutes minutes and only...");
imagettftext($im28, -115240$c3$font"$cdminutes minutes and only...");
imagettftext($im29117292$c2$font"$cdseconds seconds until...");
imagettftext($im28, -115290$c3$font"$cdseconds seconds until...");
imagettftext($im29117342$c2$font"- + $event + -");
imagettftext($im28, -115340$c3$font"- + $event + -");

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
2006-12-15 15:35:51
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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 around

Enjoy !
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_size0$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 $padding_x;
   
$box_h $box_h $padding_y;

// origin of the text = baseline of the first char
   
$text_x abs($bottom_left_x) -$padding_x;
   
$text_y $box_h -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($img200200200);
   
$grey imagecolorallocate($img,100,100,100);
   
$yellow imagecolorallocate($img0xFF0xFF0x00);
 
// attribute colors
   
$font_color $black;
   
$padding_color $lightgrey;
   
$background_color $yellow;
 
// fill image with background color
   
imagefill($img00$background_color);

// fill image with padding color
   
imagefilledrectangle($img00$box_w$padding_y 1$padding_color); // top
   
imagefilledrectangle($img00$padding_x 1$box_h 1$padding_color); // left
   
imagefilledrectangle($img0$box_h $padding_y -1$box_w$box_h 1$padding_color); // bottom
   
imagefilledrectangle($img$box_w $padding_x0$box_w 1$box_h 1$padding_color); // right

//write text
imagettftext($img$font_size0$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);
?>
2008-05-07 15:55:45
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
Автор:
That script draws Serpinski's carpet:

<?php
    set_time_limit
(5);

   
$i 4;        // Iterations
   
$xy 500;    // Picture size

   
$img imagecreatetruecolor($xy$xy);

   
$white imagecolorallocate($img255255255);

   
drawCarpet(00$xy$xy$i);
    function 
drawCarpet($a$b$c$d$n) {
        global 
$img$white;

        if(
$n <= 0) return;

       
$a1 $a $c 3;
       
$c1 $a $c 3;
       
$b1 $b $d 3;
       
$d1 $b $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);
?>
2010-03-19 18:57:51
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
Автор:
It looks like there are waves beetween the points, but it's straight.

<?php
$maxwert 
300;
$size 10;
$img imagecreatetruecolor($maxwert$maxwert);
imagecolorallocate($img000);

   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);
?>
2010-09-25 17:48:19
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
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($grandetrue);
// 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($grande000127);
// transparent alpha will be _ignored_:
imagefilledrectangle($grande00$w$h$transparent);
// ok, transparent will be used and set whole alpha channel to transparent:
imagefill($grande00$transparent);
?>
2012-08-21 12:48:27
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html
Автор:
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($im000127);
imagealphablending($imfalse);
imagefilledrectangle($im00imagesx($im) - 1$imagesy($im) - 1$transparent);
imagecolordeallocate($transparent);
imagealphablending($imtrue);
?>
2013-10-03 12:58:00
http://php5.kiev.ua/manual/ru/function.imagefilledrectangle.html

    Поддержать сайт на родительском проекте КГБ