PHP Doku:: Zeichnet ein gefülltes Rechteck - function.imagefilledrectangle.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungBildbearbeitung und GDGD- und Image-Funktionenimagefilledrectangle

Ein Service von Reinhard Neidl - Webprogrammierung.

GD- und Image-Funktionen

<<imagefilledpolygon

imagefilltoborder>>

imagefilledrectangle

(PHP 4, PHP 5)

imagefilledrectangleZeichnet ein gefülltes Rechteck

Beschreibung:

int imagefilledrectangle ( resource $im , int $x1 , int $y1 , int $x2 , int $y2 , int $col )

ImageFilledRectangle() erzeugt ein mit der Farbe col gefülltes Rechteck innerhalb des Bildes im. Die obere linke Eck-Koordinate wird mittels x1 und y1, die untere rechte Eck-Koordinate mittels x2 und y2 definiert. 0, 0 ist dabei die linke obere Ecke des Bildes im.


15 BenutzerBeiträge:
- Beiträge aktualisieren...
Tobi
25.09.2010 23:48
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);
?>
Mice32
20.03.2010 1:57
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);
?>
mike at delenk dot net
7.05.2008 21:55
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_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 text
imagettftext($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);
?>
kyperkins at shadowdev dot com
4.02.2008 11:29
This is a very simple Progress Bar using basic PHP 5.

<?php
#Created by Kyle "KP" Perkins
# of Shadow Development Technologies (http://shadowdev.com)
# on 20080204

#very simple and flexible dynamic PROGRESS BAR

#Used under the Creative Commons Share-Alike License 3.0
#http://creativecommons.org/licenses/by-sa/3.0/us/

#use: index.php?percent/total/width(px)
#example: ?s=85/100/300

#edit how you like, comments to kyperkins@shadowdev.com

header("Content-type: image/png");
extract($_GET);
$num=explode("/", $s);
$width=$num[2];
$per=($num[0]/$num[1]) * 100;
$percent=round($per, 0);
$im = @imagecreate($width, 20)
    or die(
"Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 200,200,200);
$text_color = imagecolorallocate($im, 255, 255, 255);
$fill=($percent/100)*$width;
$offset=($width/2)-($width*.02);
imagestring($im, 3, $offset, 5"$percent%", $text_color);
imagefilledrectangle($im, 0, 0, $fill, 20, imagecolorallocate($im, 0, 0, 255));
imagestring($im, 3, $offset, 5"$percent%", $text_color);
imagepng($im);
imagedestroy($im);
?>
Google
15.12.2006 22:35
<?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);
?>
info at 555webdesign dot com
25.10.2006 19:13
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?
terereese at hotmail dot com
12.07.2006 5: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
3.05.2006 12: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
15.04.2006 0: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);
}

?>
me [a.t.] forestfactory [d.o.t.] de
13.10.2005 1: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.05.2004 22: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.05.2004 19: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
18.01.2004 2: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
31.03.2002 1: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
30.11.2001 4: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)