【问题标题】:How to draw a circle with dashed border in php?如何在php中画一个带虚线边框的圆圈?
【发布时间】:2011-05-07 21:10:15
【问题描述】:

我想画一个带有虚线边框的圆圈。 imagearc 函数可用于简单的边框。但我对虚线边框没有任何办法。

谢谢。

【问题讨论】:

    标签: php graphics geometry


    【解决方案1】:

    在这里

    <?php 
    function dashedcircle($im, $cx, $cy, $radius, $colour, $dashsize=5) { 
    
       $dash=false; 
       for ($angle=0; $angle<=(180+$dashsize); $angle+=$dashsize) { 
          $x = ($radius * cos(deg2rad($angle))); 
          $y = ($radius * sin(deg2rad($angle))); 
    
          if ($dash) { 
             imageline($im, $cx+$px, $cy+$py, $cx+$x, $cy+$y, $colour); 
             imageline($im, $cx-$px, $cx-$py, $cx-$x, $cy-$y, $colour); 
          } 
          $dash=!$dash; 
          $px=$x; 
          $py=$y; 
       } 
    } 
    ?>
    

    另一种绘制虚线圆的方法。享受吧!

    <?php
    
    header("Content-type: image/jpeg");
    $im = imagecreate(100,100);
    
    $b   = imagecolorallocate ($im, 0, 0, 0);
    $w   = imagecolorallocate ($im, 255, 255, 255);
    
    $style = array ($b,$b,$b,$b,$b,$w,$w,$w,$w,$w);
    
    imagesetstyle ($im, $style);
    
    imagearc($im,50,50,100,100,0,360,IMG_COLOR_STYLED);
    
    imagejpeg($im);
    imagedestroy($im);
    ?>
    

    Reference

    【讨论】:

    • 尝试了第一个:dashedcircle($im,70,80,20,$green,1); 但只有在 cx 和 cy 相同时才有效。 viper-7.com/8zXkCK
    【解决方案2】:

    这是我用来做的代码。

    <?php
    $thick = 10;
    // create a 200*200 image
    $img = imagecreatetruecolor(200, 200);
    
    // Add antialias
    imageantialias ($img, true);
    
    // allocate some colors
    $white = imagecolorallocate($img, 255, 255, 255);
    
    // draw the dashed circle
    
    for($t = 1;$t<($thick+1);$t++) {
        for($i = 0;$i<360;$i+=10) {
            imagearc($img, 100, 100, 200-($t/5), 200-($t/5),  $i, $i+5, $white);
            imagearc($img, 100, 100, 200+($t/5), 200+($t/5),  $i, $i+5, $white);
        }
    }
    
    // output image in the browser
    header("Content-type: image/png");
    imagepng($img);
    
    // free memory
    imagedestroy($img);
    ?>
    

    【讨论】:

    • 我觉得OP已经知道怎么画圆了,他想知道怎么做虚线板
    • 通过错开角度,您可以创建一个虚线圆。查看迭代角度的 for 循环。
    • 好的,这很好用。但如果可能的话,我也想抗锯齿并改变边框的厚度。
    • 我根据 imagearc php 手册php.net/manual/en/function.imagearc.php 上的示例在其中添加了厚度,但我不确定是否添加抗锯齿。如果我发现任何东西,我会告诉你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多