简体   繁体   English

将HTML5画布作为图像保存到服务器

[英]Saving a HTML5 canvas as an image to the server

I'm trying to save my canvas as an image to my server but it is not working. 我正在尝试将画布作为图像保存到服务器,但无法正常工作。

javascript javascript

var data = canvas.toDataURL();
var picture = document.getElementById("my-picture");

PHP 的PHP

$canvas = base64_decode(str_replace('data:image/png;base64,', '' , $value['picture']));
file_put_contents('my_custom_path/my_custom_filename.png', $canvas);

this code was given to me but I am not sure how to implement the php part. 这段代码是给我的,但是我不确定如何实现php部分。

Assuming your Javascript/HTML is correct (make sure you are grabbing the correct POST value) try this PHP code instead: 假设您的Javascript / HTML是正确的(确保您获取的是正确的POST值),请改用以下PHP代码:

$data = str_replace('data:image/png;base64,', '', $data);
$data = base64_decode( str_replace(' ', '+', $data) );
$success = file_put_contents('my_custom_path/my_custom_filename.png', $data);

Also make sure the directory that you are placing the file in is accessible. 此外,请确保您可以访问放置文件的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM