If you want to make fancier polygons than you can make with imagerectangle() you can use imagepolygon() or imagefilledpolygon(). They are similar to imagerectangle() but the arguments are a little different, Instead of two x,y cooridinates, the corner points are defined by an array and there is an argument to tell the function how many points there are. The format looks like this:
imagepolygon($image, $cooridinates, $number_of_points, $color);
The image above contains a blue filled polygon and an empty red polygon. After using some math to find the points of a pentagon, I put them all in an array:
$pentagon = array(
$x1, $y1,
$x2, $y2,
$x3, $y3,
$x4, $y4,
$x5, $y5,
);
then use that array to make a filled polygon
imagefilledpolygon($image, $pentagon, 5, $blue);
To make the star, I took the same cooridinates that I used for the pentagon, rearanged them and made a new array:
$star = array(
$x1, $y1,
$x3, $y3,
$x5, $y5,
$x2, $y2,
$x4, $y4,
);
then used it to make a hollow polygon:
imagepolygon($image, $star, 5, $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);
imagegif($image);
imagedestroy($image);
?>
This example uses the color gradient technique with imagepolygon(). It loops through concentric polygons, using math functions to find the points of each new star and changing its color:
|
|
|