Home Tutorial Php Phpgd PHP GD polygon Shapes

 
 

Share on Google+Share on Google+
PHP GD polygon Shapes
Posted on: November 9, 2009 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for PHP GD polygon Shapes:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 9, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com