我使用PHP-Imagick创建了重复圆圈的图案图像,就像上面给出的示例图像一样。下面是创建上图的代码。我已经明确地评论了代码,变量名称保持相当冗长,以便了解最新情况。
$canvas = new Imagick();
$cw = 700; // user provided width
$ch = 300; //user provided height
$hrzntl_c = 10; //user provided - number of horizontal circles - min 2 and max 200
$c_diamtr = $cw / $hrzntl_c; //set the diameter of circle
$c_radius = $c_diamtr /2; //set the radius of circle
$vrtcl_c = $ch / $c_diamtr; //calculated number of vertical circles.
$canvas->newImage( $cw, $ch, new ImagickPixel( "seagreen" ) ); //create a canvas
$draw = new ImagickDraw(); // create a draw object
$draw->setFillColor( new ImagickPixel( "white" ) ); // set the fill color
for ($i = 0; $i <= $hrzntl_c; $i++){ //loop horizontal
for ($j = 0; $j <= $vrtcl_c; $j++){ //loop vertical
$draw->ellipse( $c_radius + $c_diamtr * $i, $c_radius + $c_diamtr * $j, $c_radius, $c_radius, 0, 360 );
}
}
$canvas->drawImage( $draw ); // render the circles to the canvas
$canvas->setImageFormat( "png" ); // set the image format to png
header("Content-Type: image/png"); // Output the image
echo $canvas;
问题:现在您已经知道圆圈的设计是水平对齐的。我也需要帮助来证明垂直圆圈的合理性。查看示例图像的底部,您可以看到圆圈确实不合理。通过&#34;对齐&#34;,我的意思是如果你看到一个半/整圆朝顶,那么你应该看到底部的半/全圆。就像这个完美的例子。
答案 0 :(得分:1)
根据图像高度和圆圈数计算偏移量。顺便说一句,你应该调整你的计数计算:
$vrtcl_c = ceil($ch / $c_diamtr);
$verticalOffset = ($ch - $vrtcl_c * c_diamtr) / 2;
然后,转移你的图纸:
for ($j = 0; $j < $vrtcl_c; $j++){ //loop vertical
$draw->ellipse( $c_radius + $c_diamtr * $i,
$c_radius + $c_diamtr * $j + $verticalOffset,
$c_radius, $c_radius, 0, 360 );
}