DJ Mike's Tutorials: PHP

< ^ >

Color Fills

You can fill in an area with color or fill to border like you can with ImageMagick but there is no fuzz so you can only flood exact colors. You flood with the imagefill() function and you fill to border with the imagefilltoborder(). function.

imagefill()

imagefill() has four arguments; an image resource, an x,y cooridinate pair and a color resource in this format:

<?
imagefill
($image$x$y$color);
?>


For the image above, I took the image from the previous page and added these lines of code:

<?
$purple 
imagecolorallocate$image1250125);
imagefill$image11060$purple );
imagefill$image6080$purple );
imagefill$image15080$purple );
imagefill$image80150$purple );
imagefill$image140150$purple );
?>


The four pairs of cooridinates are in the star's arms so they are filled with $purple. $purple stops filling when it gets to the red line.

<?
header
("Content-type: image/gif");
$image imagecreate220200);
$basecolor imagecolorallocate$image000);

# set colors and line thickness
$blue imagecolorallocate$image00155);
$green imagecolorallocate$image5525555);
$red imagecolorallocate$image25500);
imagesetthickness($image5);

# make a circle
$xoffset 110;
$yoffset 100;
$w 180
imagefilledellipse($image$xoffset$yoffset$w$w$green);

# find cooridinates

$deg deg2rad(108);
$x1=$xoffset+90*sin($deg);
$y1=$yoffset+90*cos($deg);

$deg deg2rad(180);
$x2=$xoffset+90*sin($deg);
$y2=$yoffset+90*cos($deg);

$deg deg2rad(252);
$x3=$xoffset+90*sin($deg);
$y3=$yoffset+90*cos($deg);

$deg deg2rad(324);
$x4=$xoffset+90*sin($deg);
$y4=$yoffset+90*cos($deg);

$deg deg2rad(36);
$x5=$xoffset+90*sin($deg);
$y5=$yoffset+90*cos($deg);

# make a pentagon
$pentagon = array(
   
$x1$y1
   
$x2$y2,
   
$x3$y3,
   
$x4$y4
   
$x5$y5
);
imagefilledpolygon($image$pentagon5$blue);

# make a star
$star = array(
   
$x1$y1
   
$x3$y3,
   
$x5$y5
   
$x2$y2,
   
$x4$y4
);
imagepolygon($image$star5$red);

### Make another color and fill star's arms
$purple imagecolorallocate$image1250125);
imagefill$image11060$purple );
imagefill$image6080$purple );
imagefill$image15080$purple );
imagefill$image80150$purple );
imagefill$image140150$purple );

imagegif($image);
imagedestroy($image);
?>

imagefilltoborder()

Instead of stopping when it hits a color different from the color that it is replacing, imagefilltoborder() keeps flooding till it gets to a border color that you define in this format:.

<?
imagefill
($image$x$y$border_color, );
?>


To make the image above, I added another color and the following line of code to the top image:

<?
$yellow 
imagecolorallocate$image2552550);
imagefilltoborder($image1010$red$yellow);
?>


Instead of stopping at the green like imagefill() would, imagefilltoborder keeps going till it gets to the border color, $red.

<?
header
("Content-type: image/gif");
$image imagecreate220200);
$basecolor imagecolorallocate$image000);

# set colors and line thickness
$blue imagecolorallocate$image00155);
$green imagecolorallocate$image5525555);
$red imagecolorallocate$image25500);

imagesetthickness($image5);

# make a circle
$xoffset 110;
$yoffset 100;
$w 180
imagefilledellipse($image$xoffset$yoffset$w$w$green);

# find cooridinates

$deg deg2rad(108);
$x1=$xoffset+90*sin($deg);
$y1=$yoffset+90*cos($deg);

$deg deg2rad(180);
$x2=$xoffset+90*sin($deg);
$y2=$yoffset+90*cos($deg);

$deg deg2rad(252);
$x3=$xoffset+90*sin($deg);
$y3=$yoffset+90*cos($deg);

$deg deg2rad(324);
$x4=$xoffset+90*sin($deg);
$y4=$yoffset+90*cos($deg);

$deg deg2rad(36);
$x5=$xoffset+90*sin($deg);
$y5=$yoffset+90*cos($deg);

# make a pentagon
$pentagon = array(
   
$x1$y1
   
$x2$y2,
   
$x3$y3,
   
$x4$y4
   
$x5$y5
);
imagefilledpolygon($image$pentagon5$blue);

# make a star
$star = array(
   
$x1$y1
   
$x3$y3,
   
$x5$y5
   
$x2$y2,
   
$x4$y4
);
imagepolygon($image$star5$red);

### Make a new color and fill star's arms
$purple imagecolorallocate$image1250125);
imagefill$image11060$purple );
imagefill$image6080$purple );
imagefill$image15080$purple );
imagefill$image80150$purple );
imagefill$image140150$purple );

### Make a new color and flood outside of star
$yellow imagecolorallocate$image2552550);
imagefilltoborder($image1010$red$yellow);

imagegif($image);
imagedestroy($image);
?>


< ^ >


Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>