downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagefilledarc> <imageellipse
[edit] Last updated: Fri, 18 May 2012

view this page in

imagefill

(PHP 4, PHP 5)

imagefillFüllen mit Farbe ("flood fill")

Beschreibung:

int imagefill ( resource $im , int $x , int $y , int $col )

ImageFill() bewirkt das Füllen eines Bildes beginnend bei der Koordinate x, y (oben links ist 0, 0) mit der Farbe col im Bild im.



imagefilledarc> <imageellipse
[edit] Last updated: Fri, 18 May 2012
 
add a note add a note User Contributed Notes imagefill
Eduard Resina 22-Jan-2011 07:12
Using imagefill with alpha colors in a png file does not allow for blending the filling with the original image. On the other hand, substituting imagefill with imagefilledrectangle in order to fill with alpha colors is only helpful when a rectangle shape is desired. This example shows how to imagefill with alpha color or alpha tile images allowing the existing background of the original image to come through, and yet filling only the area with the same color surrounding the filling start point.

<?php

$fillstartx
= 30;
$fillstarty = 80;
$r = 180;
$g = 200;
$b = 60;
$alpha = 80;
$tileimage = null;

$mainimage = imagecreatefrompng("myimage.png");
$intermediateimage = imagecreatefrompng("myimage.png"); //intermediate copy

$imagewidth = imagesx($mainimage);
$imageheight = imagesy($mainimage);

/* $tileimage = imagecreatefrompng("tile.png");  //FOR TILE IMAGEFILL */

if ($tileimage) {
   
imagesettile($intermediateimage, $tileimage);
   
$color = IMG_COLOR_TILED;
}
else
$color = imagecolorallocatealpha($intermediateimage, $r, $g, $b, $alpha);

imagealphablending($intermediateimage, false);
imagefill($intermediateimage, $fillstartx, $fillstarty, $color);
imagesettile($mainimage, $intermediateimage);  //set the filled copy as a tile to the main image

imagealphablending($mainimage, true);
imagefilledrectangle($mainimage, 0, 0, $imagewidth, $imageheight, IMG_COLOR_TILED); //apply the tile copy on top of the original

imagealphablending($mainimage, false);
imagesavealpha($mainimage, true);
imagepng($mainimage, "myimage.png", 0);

imagedestroy($intermediateimage);
imagedestroy($mainimage);

?>
Christopher Kramer 06-Oct-2009 03:00
Here is a function to generate a gradient.
You specify width, heigth and 4 colors (of the 4 corners).

An image handle of the gradient image will be returned.
You can copy the returned image onto another image using imageecopy.
This can be helpful if you want to generate a shadow for example.
("Glow effect example": Generate 8 gradients, one for each side and one for each corner. The outer side of the gradients have the background color and the inner sides have a bright color such as white.) 

But beware: This function is not optimized for performance, it might become slow on big images. For shadows, better cache the generated gradients.

Note: For gradients, using true-color is highly recommended.

<?php

function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) {

 
/*
 Generates a gradient image

 Author: Christopher Kramer
 
 Parameters:
 w: width in px
 h: height in px
 c: color-array with 4 elements:
    $c[0]:   top left color
    $c[1]:   top right color
    $c[2]:   bottom left color
    $c[3]:   bottom right color
   
 if $hex is true (default), colors are hex-strings like '#FFFFFF' (NOT '#FFF')
 if $hex is false, a color is an array of 3 elements which are the rgb-values, e.g.:
 $c[0]=array(0,255,255);
 
 */
 
 
$im=imagecreatetruecolor($w,$h);
 
 if(
$hex) {  // convert hex-values to rgb
 
for($i=0;$i<=3;$i++) {
  
$c[$i]=hex2rgb($c[$i]);
  }
 }
 
 
$rgb=$c[0]; // start with top left color
 
for($x=0;$x<=$w;$x++) { // loop columns
 
for($y=0;$y<=$h;$y++) { // loop rows
   // set pixel color
  
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
  
imagesetpixel($im,$x-1,$y-1,$col);
  
// calculate new color 
  
for($i=0;$i<=2;$i++) {
   
$rgb[$i]=
     
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
     
$c[1][$i]*($x     *($h-$y)/($w*$h)) +
     
$c[2][$i]*(($w-$x)*$y     /($w*$h)) +
     
$c[3][$i]*($x     *$y     /($w*$h));
   }
  }
 }
 return
$im;
}

function
hex2rgb($hex)
 {
 
$rgb[0]=hexdec(substr($hex,1,2));
 
$rgb[1]=hexdec(substr($hex,3,2));
 
$rgb[2]=hexdec(substr($hex,5,2));
 return(
$rgb);
 }

// usage example

$image=gradient(300, 300, array('#000000', '#FFFFFF', '#FF0000', '#0000FF'));

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

?>
dunhamzzz [A] gmail 01-Sep-2008 08:36
I feel its worth pointing out that you cannot fill a transparent colour that you have assigned with imagecolorallocatealpha, the colour will fill, but without its transparency,

I find the easiest method around this is to just fill a rectangle using imagefilledrectangle() to draw a fill instead.
daniel_nocturnal at hotmail dot com 07-Jul-2008 07:27
I've used razonklnbd's example, I had to fill an image with a background pattern from an image (file). So I put the code to pieces and come up with a lighter version that suited me, all thanks to razonklnbd.

My version receives three parameters, $image, $width, $height.

$image = The background pattern image
$width = Width of the new image
$height = Height of the new image

<?php
   
function imagefillfromfile($image, $width, $height) {
       
$imageWidth = imagesx($image);
       
$imageHeight = imagesy($image);
       
$newImage = imagecreatetruecolor($width, $height);
       
        for (
$imageX = 0; $imageX < $width; $imageX += $imageWidth) {
            for (
$imageY = 0; $imageY < $height; $imageY += $imageHeight) {
               
imagecopy($newImage, $image, $imageX, $imageY, 0, 0, $imageWidth, $imageHeight);
            }
        }
       
        return(
$newImage);
       
imagedestroy($newImage);
    }
?>

This function returns a new image with a filled canvas of an image (file) with a fixed width/height.

Example usage:

<?php
    $pattern
= imagecreatefrompng('pattern.png');
   
$image = imagefillfromfile($pattern, 400, 300);
   
imagepng($image);
   
imagedestroy($image);
   
imagedestroy($pattern);
?>
Cypog 17-Mar-2008 07:27
imagefill can't deal with alpha colors, use imagefilledrectangle instead.

<?php

    header
("Content-Type: image/png");
   
$im = imagecreatefrompng("img/button.png");
    if (empty(
$_GET['alpha']))
        {
$_GET['alpha'] = 10;}
   
$color = imagecolorallocatealpha($im, 255, 255, 255, $_GET['alpha']);
   
imagefillalpha($im, $color);
   
imagepng($im);
   
imagedestroy($im);

    function
ImageFillAlpha($image, $color)
    {
       
imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $color);
    }

?>
info at educar dot pro dot br 09-Mar-2007 07:54
Example 1
The filled region will be external to a demarcated region, if the initial coordinates will be outside of this region.

<?php
$src621
= imagecreate(200,200);
$clr_1_621 = imagecolorallocate($src621, 255, 255, 0);
$clr_2_621 = imagecolorallocate($src621, 0, 0, 250);
$clr_4_621 = imagecolorallocate($src621, 2,2,55);
imagerectangle($src621, 100, 100, 150, 150, $clr_4_621);
imagefill($src621, 110, 110, $clr_2_621);
header("Content-Type: image/png");
imagepng ($src621);
imagedestroy($src621);
?>

Example 2
The filled region will be internal to a demarcated region, if the initial coordinates will be inside of this region.
<?php
$src622
= imagecreate(200,200);
$clr_1_622 = imagecolorallocate($src622, 255, 255, 0);
$clr_2_622 = imagecolorallocate($src622, 0, 0, 250);
$clr_4_622 = imagecolorallocate($src622, 2,2,55);
imagerectangle($src622, 100, 100, 150, 150, $clr_4_622);
imagefill($src622, 10, 10, $clr_2_622);
header("Content-Type: image/png");
imagepng ($src622);
imagedestroy($src622);
?>

See another examples at:
http://www.educar.pro.br/a/gdlib/index.php?pn=50&tr=97
Gromitt 06-Mar-2007 10:14
If you need to fill a whole image (immediatly after its creation for instance), consider applying a filled rectangle instead, using imagefilledrectangle() :

<?php

$gdImage
= imagecreatetruecolor(100, 100);
$gdColor = imagecolorallocate($gdImage, 255, 0, 0); // red
imagefilledrectangle($gdImage, 0, 0, 99, 99, $gdColor);

?>

which will require much less logic and processing from GD.
razonklnbd at yahoo dot com 24-Sep-2006 03:32
I spend more then two hour to find a function that can fill a pattern or file as background instead of color. but i can't find. so i develop the following function. i though this function will save time who need it...

Function will get four parameter
1. Main Image Identifier
2. Pattern Image Identifier
3. Final Image Width
4. Final Image Height

If you set final image width or height is less then main image width or height then you may get wrong result

<?php

function fill_with_patternfile($p_main_im, $p_patternfile_im, $p_width, $p_height){
   
$pimiX=$p_patternfile_im;
   
$pw=imagesx($pimiX);
   
$ph=imagesy($pimiX);
   
$targetImageIdentifier=imagecreatetruecolor($p_width,$p_height);
    if(
$pw<$p_width && $ph<$p_height){
        for(
$pX=0;$pX<$p_width;$pX+=$pw){
            for(
$pY=0;$pY<$p_height;$pY+=$ph){
               
imagecopy($targetImageIdentifier,$pimiX,$pX,$pY,0,0,$pw,$ph);
            }
        }
    }else
imagecopy($targetImageIdentifier,$pimiX,0,0,0,0,$pw,$ph);
   
$w=imagesx($p_main_im);
   
$h=imagesy($p_main_im);
   
$nX=0;
    if(
$w<$p_width) $nX=intval(($p_width-$w)/2);
   
$nY=0;
    if(
$h<$p_height) $nY=intval(($p_height-$h)/2);
   
imagecopy($targetImageIdentifier,$p_main_im,$nX,$nY,0,0,$w,$h);
    return
$targetImageIdentifier;
}
//    If you want to use a gif or png file as
//    pattern file you need to change function below :)
$pattern_im=imagecreatefromjpeg('logo.jpg');
//    If you want to use a gif or png file as
//    main file you need to change function below :)
$main_im=imagecreatefromjpeg('r2.jpg');
//    call the function width 500 and height 500
//    if your width and height is less then main images
//    width and height then you can't understand any change!
$final=fill_with_patternfile($main_im, $pattern_im, 500, 500);
//    view the image and destroy all instance
header('Content-type: image/jpeg');
imagejpeg($final);
imagedestroy($final);
imagedestroy($main_im);
imagedestroy($pattern_im);
exit();

?>
jonathan dot aquino at gmail dot com 08-Jun-2006 03:12
Use imageSaveAlpha($image, true); to preserve transparency.
gelak 29-Nov-2005 11:27
//A smiley face ;]

<?php

header
('Content-type: image/png');

$smile=imagecreate(400,400);
$kek=imagecolorallocate($smile,0,0,255);
$feher=imagecolorallocate($smile,255,255,255);
$sarga=imagecolorallocate($smile,255,255,0);
$fekete=imagecolorallocate($smile,0,0,0);
imagefill($smile,0,0,$kek);

imagearc($smile,200,200,300,300,0,360,$fekete);
imagearc($smile,200,225,200,150,0,180,$fekete);
imagearc($smile,200,225,200,123,0,180,$fekete);
imagearc($smile,150,150,20,20,0,360,$fekete);
imagearc($smile,250,150,20,20,0,360,$fekete);
imagefill($smile,200,200,$sarga);
imagefill($smile,200,290,$fekete);
imagefill($smile,155,155,$fekete);
imagefill($smile,255,155,$fekete);
imagepng($smile);

?>
pisu at estmail dot hu 29-Nov-2005 01:48
chess board
<?php

$kep
= imagecreate(400,400);
$fekete = imagecolorallocate($kep,0,0,0);
$feher = imagecolorallocate($kep,255,255,255);

//imagefill($kep,50,50,$fekete);
imagefill($kep,50,20,$feher);

for (
$i=1;$i<8;$i++)  {
 
$k = $i*50;
 
imageline($kep,0,$k,400,$k,$fekete);
 
imageline($kep,$k,0,$k,400,$fekete);
  }

for (
$i=0;$i<8;$i++)  {
  for (
$j=0;$j<8;$j++)  {
   
$x = $i*50 + 2;
       
$y = $j*50 + 2;
    if (  ( (
$i % 2) + ($j % 2) )  % 2 == 0 )  {
         
imagefill($kep,$x,$y,$fekete);
          }

    }
  }

// imagecolortransparent($kek,$piros);

header('content-type : image/png');
imagepng($kep);

?>
colin at galaxyinteractive dot net 28-Jul-2005 08:26
Didn't see this documented, although it's outlined in imagefilledrectangle, it wasn't quite so obvious to me at first

imageSetTile($image,$imageBack);
imagefill($image,0,0,IMG_COLOR_TILED);

Will fill an image with a texture (this is great as I'm building a logo/template creator)
Igor Garcia 28-Jun-2003 03:18
This function, cannot deal with transparencies.
So you need to use imagecolorallocate instead of imagecolorallocatealpha.
Thus, be careful with color variables that allready set with imageallocatecoloralpha because this can slow-down or hang-up your system.
norxh 04-Jun-2003 08:36
For new images, you must allocate a color before this function will work.
08-Feb-2003 05:49
Actually, it can handle pre-transparent images. To remove it you need to do something like:
   imagecolortransparent($img, 0);
to null out the previous transparency colors. ;)
aqmprod at iname dot com 09-Jan-2000 05:08
This function does not seem to work with images already been transparent. If you fill at x=0, y=0, and there are still transparent parts that you did
not reach with your fill, they change to a different color.

The ImageColorSet function seems to be the solution, but i can't work with transparancy.

 
show source | credits | sitemap | contact | advertising | mirror sites