I have a problem saving a png image with php. I followed the suggestion of @drew010 as described here: https://stackoverflow.com/a/11511605/9117408 But when i try to open the saved image i got the error used as title of my question
Here is the JS code to send the image to php:
canvas.className = "photo-canvas";
canvas.width = camera.videoWidth;
canvas.height = camera.videoHeight;
canvas.getContext('2d').drawImage(camera, 0, 0);
if (hat.checked)
{
console.log(canvas.toDataURL());
var xhttp = new XMLHttpRequest();
xhttp.open("POST","backend/functions.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("photo="+canvas.toDataURL());
...
}
And here the php code that process and save the image:
$data = $_POST['photo'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents("$_SERVER[DOCUMENT_ROOT]/userphoto/".getdate()[0]."_".$_SESSION['user']['iduser'].".png", $data);
The error i get when i try to open the image from my computer
I tried to open the base64 image directly into browser and I was able to successfully see the image. So do i have problems with library or i'm doing something wrong? Thanks