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



PHP : Function Reference : Image Functions : imagefill

imagefill

Flood fill (PHP 4, PHP 5)
bool imagefill ( resource image, int x, int y, int color )

Example 1007. imagefill() example

<?php

$im
= imagecreatetruecolor(100, 100);

// sets background to red
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);

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

The above example will output something similar to:


Related Examples ( Source code ) » imagefill















Code Examples / Notes » imagefill

jonathan dot aquino

Use imageSaveAlpha($image, true); to preserve transparency.

igor garcia

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.


aqmprod

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.


gromitt

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

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


norxh

For new images, you must allocate a color before this function will work.

info

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


colin

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)


pisu

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


08-feb-2003 01: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. ;)


gelak

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


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