I am following this example to save the canvas to a PNG file. http://greenethumb.com/article/1429/user-friendly-image-saving-from-the-canvas/
My problem: The downloaded file is corrupt and when I open it in notepad, it has this pattern:
- HTML CODE (Correspoding to the PHP file)
- ASCII CHARACTERS (which I thought corresponded to the PNG file)
<body><html>
If I remove #1 and #3 and save the file locally, I get a valid PNG image.
I am doing exactly what the example above says, but my results are different. Why would the dataURL have any other info. other than the canvas itself?
Thanks.
Edit
<?php
$imdata = $_POST["imgdata"];
//run this code only when there is long POST data
if(strlen($imdata)>1000) {
//removing the "data:image/png;base64," part
$imdata = substr($imdata,strpos($data,",")+1);
// put the data to a file
file_put_contents('image.png', base64_decode($imdata));
//force user to download the image
if(file_exists("image.png")){
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="image.png"');
readfile('image.png');
}
}
?>