Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagefilledrectangle

imagefilledrectangle

Draw a filled rectangle (PHP 4, PHP 5)
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.

Parameters

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()

Return Values

Returns TRUE on success or FALSE on failure.

Related Examples ( Source code ) » imagefilledrectangle





Code Examples / Notes » imagefilledrectangle

tmg

you can also add the rating til to the image

info

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?


saramg

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.


michal dot kocarek

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


administrador ensaimada sphoera punt com

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


terereese

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


sdonie

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


ivank

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


me a.t. forestfactory d.o.t. de

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.

booga

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


spam

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


google

<?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(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); //Text
imagefilledrectangle($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 text
imagettftext($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);
?>


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
imagechar
imagecharup
imagecolorallocate
imagecolorallocatealpha
imagecolorat
imagecolorclosest
imagecolorclosestalpha
imagecolorclosesthwb
imagecolordeallocate
imagecolorexact
imagecolorexactalpha
imagecolormatch
imagecolorresolve
imagecolorresolvealpha
imagecolorset
imagecolorsforindex
imagecolorstotal
imagecolortransparent
imageconvolution
imagecopy
imagecopymerge
imagecopymergegray
imagecopyresampled
imagecopyresized
imagecreate
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker