2

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

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

Andrea
  • 134
  • 2
  • 12
  • 1
    Check the file with `pngcheck` and see if it gives a more useful error message (although it might not, it's still worth checking). Can you upload one of these faulty images somewhere and provide a link? – Jongware Apr 28 '18 at 17:03
  • @usr2564301 i can do that But I think I have some problems with the libraries – Andrea Apr 29 '18 at 17:54

1 Answers1

0

For this case just go to the image file in the file system and open the image file with some image editing tool like "Gimp".

And no need to make any changes to the image, just export it with the same name and replace the existing image.

Now you will not get the error while opening the image.

Tarun M
  • 31
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 07 '21 at 08:37