DJ Mike's Tutorials: PHP

< ^ >

Polygons

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

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

Color Gradient Polygons

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:



Show Image Show Source Code

Show Image Show Source Code

< ^ >


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>