I'm working with AJAX, and I sent a request with POST, in that request I sent a canvas generated image (dataURL). When request is finished, returns in console the generated image in base64. I take all string of image data and I go to a webpage (https://codebeautify.org/base64-to-image-converter), in that page I put the image data, and well I get a the image generated perfectly.
Image Data in base64 to image converter
The problem is that the function file_put_contents writes a corrupted file. (In addition, the file weighs more than 0 bytes).
Well, I don't have any idea what I do wrong. Finally, here is the code and sorry for my bad english. Thanks :)
uploadThumbnail.php
<?php
session_start();
$img = $_REQUEST['imgBase64'];
$img = htmlspecialchars($img);
echo $img;
file_put_contents("../thumbnails/" . $_SESSION['uploadedVideoID'] .'.png',
base64_decode($img));
?>
And, Caller script.
function generateThumbnail() {
var thecanvas = document.getElementById('taco');
var context = thecanvas.getContext('2d');
context.drawImage(video, 0, 0, 256, 144);
var dataURL = thecanvas.toDataURL();
//create img
var img = document.createElement('img');
img.setAttribute('src', dataURL);
//append img in container div
document.getElementById('videoThumbnailContainer').appendChild(img);
$.ajax({
type: "POST",
url: "upload/uploadThumbnail.php",
data: {
imgBase64: dataURL
}
}).done(function(o) {
console.log(o);
});
}