在PHP中的两点之间绘制一条曲线

在PHP中的两点之间绘制一条曲线

问题描述:

I want to draw a simple curved line between two points. More specifically, the top left and bottom right corner of an image of arbitrary size.

I tried using imagearc, but apparently that's not what I'm looking for. To illustrate what I mean: curved line

I can't find any function to help me along, so any help would be appreciated :)

我想在两点之间绘制一条简单的曲线。 更具体地说,任意大小的图像的左上角和右下角。 p>

我尝试使用imagearc,但显然这不是我正在寻找的。 说明我的意思: p>

我可以 找不到任何帮助我的功能,所以任何帮助都会受到赞赏:) p> div>

I solved it using imagearc after all.

The trick is to set the bottom left corner as the center, -90° start angle, 0° end angle and double the size of the image:

//GET VARS
$width = $_GET['width'];
$height = $_GET['height'];

//CREATE IMGS
$image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($image, 255, 0, 0);

imagearc(    $image,
             0, 0, //center point = bottom-left corner
             $width*2, $height*2, //size = image size * 2
             -90, //top left
             0, //bottom right
             $color);


//OUTPUT IMAGE
header('Content-Type: image/png');
imagepng($image);

//DESTROY IMAGE
imagedestroy($image);

Looks like this: http://www.schizosplayground.com/pers/curvedlinetest.php?width=132&height=163

You could use ImageMagick instead of image gd. Image gd has no build-in support for curves.

If you don't have the possibility to use ImageMagick, you could still use imagesetpixel and create your own curve with a simple de casteljau algorithm

I solved a similar problem by generating a vector with points ($polygon) via any convinient function and then drew a lines inbetween the points:

$numberofpoints=count($polygon)/2-1; // XY coordinates, so points is just half and subtracting the end point
for ($i=0; $i < $numberofpoints;$i++) {
     imageline($image,  $polygon[2*$i], $polygon[2*$i+1], $polygon[2*$i+2], $polygon[2*$i+3],  $Color); // connect two consecutive points with a line
}