PHP GD polygon Shapes


 

PHP GD polygon Shapes

This example shows how to make polygon Shapes in php gd Library.

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

0

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

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

}

1

return $points;

}

 

2

?>

After running the program you will get the following output

Ads