I\'m rendering a screenshot with html2canvas 0.4.0 and want to save it as image on my webserver.
To do so, I\'ve written the following function:
In case someone stumbles over the same problem, here is how I solved it:
The problem depended on the fact, that +
in URLs is interpreted as an encoded space (like %20
) by most servers. So I needed to encode the data first and then send it via POST
to my designated PHP function.
Here is my code:
function screenShot(id) {
html2canvas(id, {
proxy: "https://html2canvas.appspot.com/query",
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png");
var output = encodeURIComponent(img);
var Parameters = "image=" + output + "&filedir=" + cur_path;
$.ajax({
type: "POST",
url: "inc/savePNG.php",
data: Parameters,
success : function(data)
{
console.log("screenshot done");
}
}).done(function() {
//$('body').html(data);
});
}
});
}
<?php
$image = $_POST['image'];
$filedir = $_POST['filedir'];
$name = time();
$image = str_replace('data:image/png;base64,', '', $image);
$decoded = base64_decode($image);
file_put_contents($filedir . "/" . $name . ".png", $decoded, LOCK_EX);
echo $image;
?>
Cheers!