PHP GD 函数从数组中绘制折线


php gd function to draw polyline from array

也许你可以帮我。我需要一个函数来从_GET或_POST字符串绘制折线路径并将生成的图像保存到文件夹中。例如,我的链接如下所示:http://img.domain.com/?points = 1,5,-70,300,250,500...如果图像已经生成并且没有更改 ->从文件夹中加载它。否则生成新的。

我的代码在这里:

if (isset($_POST['points'])) {
   $points = $_POST['points'];

   $image = imagecreate(200, 200);
   $white = imagecolorallocate($image, 255, 255, 255);
   $black = imagecolorallocate($image, 0,   0, 0);
    ... polyline path drawing here...?
   imageline($image,  10,   10,  10, 190,   $black);
   header('Content-Type: image/png');
   imagepng($image);
   imagedestroy($image);
   ... how to save it to the server?
}

谢谢。

要保存图像,您可以使用第二个(可选)参数 imagepng

imagepng($image, 'saved.png');

对于折线,您将在循环中调用imageline - 具体如何取决于您$points值的结构。

若要动态将图像保存到服务器,请使用图像函数的第二个参数指定位置和文件名。

//specify the path on the server where you want to save the image
$path_image = 'saved-example.png';
imagepng($image, $path_image);
imagepng($image);
imagedestroy($image);

图像将保存到该路径。