imagefilledrectangle

(PHP 3, PHP 4, PHP 5)

imagefilledrectangle -- Draw a filled rectangle

Description

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

imagefilledrectangle() creates a filled rectangle of color color in image image starting at upper left coordinates x1, y1 and ending at bottom right coordinates x2, y2. 0, 0 is the top left corner of the image.



imagefilledrectangle
terereese at hotmail dot com
11-Jul-2006 08:02
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
administrador(ensaimada)sphoera(punt)com
03-May-2006 03:33
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
spam at junk dot dk
14-Apr-2006 03:42
A different kind of rating bar, using michal's rounded corners function :) I saw this type of rating bar somewhere and thought it looked cool, so the 'design' is a complete ripoff.

Also, note that it scales quite well, from tiny 3x3 pixel dots to huge 300x300 pixel blocks. (Man am I proud of this thing.. well, enjoy!)

/Scarpia

<?php

Header
("Content-type: image/png");
drawRatingBlocks($rating);

function
drawRatingBlocks($rating) {
  
$rating = $_GET['rating'];
  
$width = $_GET['width'];
  
$height = $_GET['height'];
  
$blocks = $_GET['blocks'];

   if (
$width == 0) $width = 130;
   if (
$height == 0) $height = 13;

  
$spacing = max(1, floor($height/6) );

   if (
$blocks == 0) $blocks = floor( $width / ($height-2 + $spacing) );

  
$blockwidth = floor( $width / $blocks ) - $spacing;
  
$short_side = min($height-2, $blockwidth);
  
$blockvalue = 100 / $blocks;

  
$image = ImageCreate($width,$height);
  
$img2 = ImageCreate($width,$height);

  
// Define base colors
  
$back = ImageColorAllocate($image,255,255,255);
  
$blur = ImageColorAllocate($image,230,223,188);
  
$fill = ImageColorAllocate($image,188,182,133);
  
$lightblur = ImageColorAllocate($image,238,235,216); // Would be nice to calculate these dynamically instead
  
$lightfill = ImageColorAllocate($image,221,217,193); // Would be nice to calculate these dynamically instead
  
ImagePaletteCopy($img2,$image);

  
ImageFilledRectangle($image,0,0,$width-1,$height-1,$back);
  
ImageFilledRectangle($img2,0,0,$width-1,$height-1,$back);
  
   if (
$short_side < 25)
      
$radius = 1;
   else if (
$short_side < 40)
      
$radius = 3;
   else if (
$short_side < 50)
      
$radius = 5;
   else
      
$radius = floor($height/10);

   for(
$i=0;$i<$blocks;$i++) {
      
$startX = ($blockwidth+$spacing)*$i + 1;
       if ((
9 <= $short_side) && ($short_side <= 16))
       {
          
ImageFilledRectangle($image, $startX, 1, $startX+$blockwidth-1, $height-2, $lightblur);
          
ImageFilledRectangle($img2, $startX, 1, $startX+$blockwidth-1, $height-2, $lightfill);
       }
      
ImageRectangleWithRoundedCorners($image, $startX, 1, $startX+$blockwidth-1, $height-2, $radius, $blur);
      
ImageRectangleWithRoundedCorners($img2, $startX, 1, $startX+$blockwidth-1, $height-2, $radius, $fill);
   }

  
$barwidth = ($rating/100)*$blocks*$blockwidth;
  
$barwidth += floor($barwidth/$blockwidth)*$spacing+1;

  
ImageCopy( $image, $img2, 0, 0, 0, 0, $barwidth, $height );
  
ImageDestroy($img2);

  
ImagePNG($image);
  
ImageDestroy($image);
}

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);
}

?>
TMG
01-Feb-2006 06:18
you can also add the rating til to the image
me [a.t.] forestfactory [d.o.t.] de
12-Oct-2005 04:44
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.
michal dot kocarek at seznam dot cz
30-May-2004 01:18
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);
}

?>
sdonie at lgc dot com
27-May-2004 10:19
I made some simple additions to the 'ratings bar' example above to allow for different sizes. It also doesn't assume that global GET variables is turned on, and explicitly looks for parameters it needs.

----
<?php

// copied from the PHP manual:
// http://us3.php.net/manual/en/function.imagefilledrectangle.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="makebar.php?rating=25.2&width=200&height=20" border="0">
// rating is a percentage from 0 to 100.

function drawRating($rating) {
  
$width = $_GET['width'];
  
$height = $_GET['height'];
   if (
$width == 0) {
    
$width = 102;
   }
   if (
$height == 0) {
    
$height = 10;
   }

  
$rating = $_GET['rating'];
  
$ratingbar = (($rating/100)*$width)-2;

  
$image = imagecreate($width,$height);
  
//colors
  
$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,$width-1,$height-1,$back);
  
ImageFilledRectangle($image,1,1,$ratingbar,$height-1,$fill);
  
ImageRectangle($image,0,0,$width-1,$height-1,$border);
  
imagePNG($image);
  
imagedestroy($image);
}

Header("Content-type: image/png");
drawRating($rating);

?>
booga
17-Jan-2004 05:48
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);

?>
ivank at 2xtreme dot net
30-Mar-2002 04:34
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);
saramg at uclink dot berkeley dot edu
29-Nov-2001 07:42
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.

<imagefilledpolygonimagefilltoborder>
 Last updated: Tue, 15 Nov 2005