将大量画布图像保存到服务器

时间:2014-01-06 08:51:38

标签: javascript html html5-canvas

我有一个脚本可以从canvas.toDataURL()生成大约250张图片。 我的问题是如何一次性保存它们(一个请求到服务器)而不是单独发布它们。

我需要一种方法将它们打包在单个请求中,然后将它们发送到PHP服务器以进行解包。

1 个答案:

答案 0 :(得分:0)

canvas.toDataURL()给出了用base64编码的字节数据,它们可以安全地用作字符串并通过HTTP传递。

做类似的事情:

var images = [];
while(....) {
    images.push(canvas.toDataURL());
}
var imagesJson = JSON.stringify(images);
// pseudo code of jQuery ajax, you can make use of form and a hidden field 
// or any other way you'd like to query the server
$.ajax({
....
send:{images:imagesJson}
....
});

使用PHP(其他服务器端语言应该几乎相同):

$images = json_decode($_POST['images'], true);
foreach($images as $image) {
    $image = base64_decode($image);
    file_put_contents('<path to file>', $image);
}
相关问题