I am drawing an image in a canvas and passing the canvas dataURL through AJAX to a PHP code to store it in Mongo DB gridFS.
While I am able to get the data URL and able to store the file but when I retrieve to show the image it does not show the image. My code snippet for storing is as below
$image = str_replace('data:image/jpeg;base64,', '', $_POST['dataurl']);
$decodedData = base64_decode($image);
try{
/*Get DB Connection for catagories document*/
$db = $this->registry->db->selectDB('myfiles');
// GridFS
$grid = $db->getGridFS();
$gridMetadata = array();
$gridMetadata['userid'] = $_POST['userid'];
$id = $grid->storeBytes($decodedData, $gridMetadata);
}
catch(MongoGridFSException $e){
echo $e;
}
to Display the image the code is
<?php
// Connect to Mongo and set DB and Collection
$mongo = new Mongo();
$db = $mongo->myfiles;
$gridFS = $db->getGridFS();
$image = $gridFS->findOne("IMG_0808.JPG");
header('Content-type: image/JPG');
echo $image->getBytes();
$mongo->close();
?>
I think I am not storing the file right. Where am I going wrong. Any help would be greatly appreciated