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() 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( $image, 125, 0, 125);
imagefill( $image, 110, 60, $purple );
imagefill( $image, 60, 80, $purple );
imagefill( $image, 150, 80, $purple );
imagefill( $image, 80, 150, $purple );
imagefill( $image, 140, 150, $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 = imagecreate( 220, 200);
$basecolor = imagecolorallocate( $image, 0, 0, 0);
# set colors and line thickness
$blue = imagecolorallocate( $image, 0, 0, 155);
$green = imagecolorallocate( $image, 55, 255, 55);
$red = imagecolorallocate( $image, 255, 0, 0);
imagesetthickness($image, 5);
# 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, $pentagon, 5, $blue);
# make a star
$star = array(
$x1, $y1,
$x3, $y3,
$x5, $y5,
$x2, $y2,
$x4, $y4,
);
imagepolygon($image, $star, 5, $red);
### Make another color and fill star's arms
$purple = imagecolorallocate( $image, 125, 0, 125);
imagefill( $image, 110, 60, $purple );
imagefill( $image, 60, 80, $purple );
imagefill( $image, 150, 80, $purple );
imagefill( $image, 80, 150, $purple );
imagefill( $image, 140, 150, $purple );
imagegif($image);
imagedestroy($image);
?>
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( $image, 255, 255, 0);
imagefilltoborder($image, 10, 10, $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 = imagecreate( 220, 200);
$basecolor = imagecolorallocate( $image, 0, 0, 0);
# set colors and line thickness
$blue = imagecolorallocate( $image, 0, 0, 155);
$green = imagecolorallocate( $image, 55, 255, 55);
$red = imagecolorallocate( $image, 255, 0, 0);
imagesetthickness($image, 5);
# 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, $pentagon, 5, $blue);
# make a star
$star = array(
$x1, $y1,
$x3, $y3,
$x5, $y5,
$x2, $y2,
$x4, $y4,
);
imagepolygon($image, $star, 5, $red);
### Make a new color and fill star's arms
$purple = imagecolorallocate( $image, 125, 0, 125);
imagefill( $image, 110, 60, $purple );
imagefill( $image, 60, 80, $purple );
imagefill( $image, 150, 80, $purple );
imagefill( $image, 80, 150, $purple );
imagefill( $image, 140, 150, $purple );
### Make a new color and flood outside of star
$yellow = imagecolorallocate( $image, 255, 255, 0);
imagefilltoborder($image, 10, 10, $red, $yellow);
imagegif($image);
imagedestroy($image);
?>
|
|
|