使用imagefilledpolygon绘制星形(带图)

时间:2010-08-22 04:16:15

标签: php geometry gd

PHP GD是否有办法使用imagefilledpolygon绘制星标?

在哪里绘制点?

我相信它涉及正弦和余弦,因为......

alt text

如何在GD中使用 sine cosine 将这些点与中心相关联?

3 个答案:

答案 0 :(得分:3)

是肯定的。我建议你阅读你自己提供的手册,因为它准确地告诉你你需要做什么。它甚至给出了一个三点星的例子,甚至在一个五边星的用户笔记中也有一个例子。

答案 1 :(得分:2)

很容易计算出你需要的点数:

<?php
function drawStar($img,$x,$y,$radius,$sides,$color,$spikness=0.5)
{
$point =array();
$t = 0;
for($a = 0;$a <= 360;$a += 360/($sides*2))
{
$t++;
if($t % 2 == 0)
{
$point[] = $x + ($radius * $spikness) * cos(deg2rad($a));
$point[] = $y + ($radius * $spikness) * sin(deg2rad($a));
}else{
$point[] = $x + $radius * cos(deg2rad($a));
$point[] = $y + $radius * sin(deg2rad($a));
}
}
return imagefilledpolygon($img,$point,$sides*2,$color);
}
?>

答案 2 :(得分:0)

在绘制形状之前,在php.in文件中添加DG Liabrary。 这是代码:

<?php
   // Create a 200 x 200 image
   $canvas = imageCreateTrueColor(800, 800);

   // Allocate colors
   $pink = imageColorAllocate($canvas, 255, 105, 180);
   $white = imageColorAllocate($canvas, 255, 255, 255);
   $green = imageColorAllocate($canvas, 132, 135, 28);

   imageLine($canvas, 40, 50, 130, 150, $white);        //draw a line
   imagerectangle($canvas, 150, 50, 250, 150, $pink);   //draw a rectange 
   filled with color
   imageFilledRectangle($canvas, 300, 50, 500, 150, $green);//draw a rectangle
   imageellipse($canvas, 600, 100, 100, 100, $green);   //draw a circle
   $points = array (   76,  228,    // Point 1 (x, y)
                105, 228,   // Point 2 (x, y)
                117, 197,   // Point 3 (x, y)
                129, 228,   // Point 4 (x, y)
                156, 228,   // Point 5 (x, y)
                135, 246,   // Point 6 (x, y)
                149, 285,   // Point 7 (x, y)
                117, 260,   // Point 8 (x, y)
                82, 288,    // Point 9 (x, y)
                98, 245     // Point 10 (x, y)
            );
   imagefilledpolygon( $canvas, $points, 10, $white );//draw a star with polygon()


   // Output and free from memory
   header('Content-Type: image/png');

  imagepng($canvas);
  imagedestroy($canvas);
?>