Object to json is easy but don't know what happening with my code.
So i wanted to read image metadata using PHP ( most important is image created date-time ) but iPhone and other phone do not send this info with image, its working if try to upload using PC. but if image is compressed then we lost all metadata.
So it's not useful to read metadata using php then i decided to read via JavaScript and for that i am using exif.js.
for multiple upload everything is working fine but when i try to convert object to json, i am getting empty string.
Here is my code
$('#ServicePhotos').change(function(){
$('#err_msg').html('');
var objects = {};
var lg = this.files.length;
for(var i = 0; i < lg; i++){
(function(file){
var file = file;
var file_path = file.name;
if(file_path){
var startIndex = (file_path.indexOf('\\') >= 0 ? file_path.lastIndexOf('\\') : file_path.lastIndexOf('/'));
var filename = file_path.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1); }
//console.log('uploading image ' + filename);
}
EXIF.getData(file, function() {
var exifData = EXIF.pretty(this);
if (exifData) {
var allMetaData = EXIF.getAllTags(this);
var DateTime = EXIF.getTag(this, "DateTime");
objects[filename] = {'created':DateTime};
}
});
})(this.files[i]);
}
console.log(objects); /*showing all data */
var myJSON = JSON.stringify(objects);
console.log(myJSON); /* EMPTY ??? */
$('#err_msg').html(myJSON);
$('#meta_data').val(myJSON);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.js"></script>
<input id="ServicePhotos" type="file" />
<div id="err_msg"></div>
Now here "var myJSON" is NOT showing json data
Here is console.log
</div>