I'm using html2canvas to save a div as an image in my directory. This div has dynamic information that is defined through a form. I have a button that when clicked runs the function and saves the image as div-saved.jpeg. So far so good, the image is saved.
The problem is that several times a day I need to change this information and save it with the same name (because this image is displayed on another page), but after three changes the image stops being replaced in the directory and I need to delete it manually to get it working again.
Here are the codes:
script:
function doCapture() {
window.scrollTo(0, 0);
html2canvas(document.getElementById("div-to-save")).then(function (canvas) {
var ajax = new XMLHttpRequest();
ajax.open("POST", "save-capture.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send("image=" + canvas.toDataURL("image/jpeg", 0.9));
ajax.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
});
}
save-capture.php:
<?php
$image = $_POST["image"];
$image = explode(";", $image)[1];
$image = explode(",", $image)[1];
$image = str_replace(" ", "+", $image);
$image = base64_decode($image);
file_put_contents("div-saved.jpeg", $image);
echo "Done";
Does anyone know what may be happening?