javascript - HTML5 Canvas created JPEG is "corrupt" or "damaged"? -
i have simple html5 canvas object that, once saved file, opens perfeclty in photoshop , looks correct, according windows , osx "corrupted" or "damaged".
the code i'm using simple:
javascript:
var dataurl = canvas.todataurl({ format: 'jpeg', quality: 0.8 });
and sending dataurl
via ajax:
$.ajax({ type: "post", url: "scripts/saveimage.php", data: { imgbase64: dataurl } })
to simple php script:
$imagebase64 = $_post['imgbase64']; $imagedata = base64_decode($imagebase64); file_put_contents($filename,$imagedata);
the file created file. can open in photoshop , looks expected. hex editor shows has jfif
in headers, you'd expect jpeg, too:
but can't open file in windows or osx preview:
have done wrong? there in file header that's throwing off windows/osx? or expected behavior jpeg created in way?
i had same problem, turned out "dataurl" started data:image/png;base64,
confusing reader programs such windows media viewer etc.
to solve problem used code
$file = base64_decode(str_replace(' ', '+', str_replace('data:image/png;base64,', '', \input::data('base64image'))));
i guess have change to
$file = base64_decode(str_replace(' ', '+', str_replace('data:image/jpeg;base64,', '', $_post['imgbase64'])));
honestly, don't remember purpose of first str_replace
, reason i've included in answer, because may break if removed it.
Comments
Post a Comment