Home Tutorial Php Phpgd PHP GD polygon Shapes

 
 

PHP GD polygon Shapes
Posted on: November 9, 2009 at 12:00 AM
This example shows how to make polygon Shapes in php gd Library.

<?php

$points = shape(8,100);

$points = translatePolygon($points,100,100);

 

$image = imagecreatetruecolor(200,200);

$white = imagecolorallocate($image,255,255,255);

imagefill($image,0,0,$white);

$red = imagecolorallocate($image,150,0,0);

imagefilledpolygon($image,$points,(count($points)/2),$red);

 

header("Content-type: image/jpeg");

imagejpeg($image);

imagedestroy($image);

 

function shape($number_of_edges,$radius=100) {

$p = array();

$degrees = 360/$number_of_edges;

for ($i=0;$i<$number_of_edges;$i++) {

$cos = cos(deg2rad($degrees*$i));

$sin = sin(deg2rad($degrees*$i));

$x = 0;

$y = $radius;

$p[] = round($cos*($x) - $sin*($y));

$p[] = round($sin*($x) + $cos*($y));

}

return $p;

}

 

function translatePolygon($points,$x=0,$y=0) {

for($i=0;$i<count($points);$i+=2) {

$points[$i] = $points[$i] + $x;

$points[$i+1] = $points[$i+1] + $y;

}

return $points;

}

 

?>

After running the program you will get the following output

Related Tags for PHP GD polygon Shapes:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.