I've read a .png
image file into an Array Buffer.
var readSingleFile = function(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents =new Int8Array(e.target.result);
$.ajax({
url: 'saveBinaryData.php',
type: 'POST',
contentType: 'application/octet-stream',
data: contents,
processData: false
});
};
reader.readAsArrayBuffer(file);
}
This is posted to a PHP file as the AJAX describes.
How do I convert this binary data to an image format?
My code so far:
<?php
if($data=file_get_contents($_POST)){
echo "Error! Data not read!";
return;
}
$data = imagecreatefromstring($data);
var_dump($data);
file_put_contents("recentImage1.png", $data);
?>
These are taken from various solutions found in Google. The image file is created but does not hold any content and therefore cannot be opened.