Michael
Michael

Reputation: 33297

Retrieve EXIF Image Meta Data from HTML5 FileApi loaded Image?

I am using the HTML5 File API & FileReader.

HTML:

<div id="holder"></div> 

JS:

<script>
var holder = document.getElementById('holder'),
    state = document.getElementById('status');

if (typeof window.FileReader === 'undefined') {
  state.className = 'fail';
} else {
  state.className = 'success';
  state.innerHTML = 'File API & FileReader available';
}

holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
  this.className = '';
  e.preventDefault();

  var file = e.dataTransfer.files[0],
      reader = new FileReader();
  reader.onload = function (event) {
    console.log(event.target);
    holder.style.background = 'url(' + event.target.result + ') no-repeat center';
  };
  console.log(file);
  reader.readAsDataURL(file);

  return false;
};
</script>

How can I retrieve EXIF meta data from the uploaded image?

I tried to use this.

HTML:

<img src="image1.jpg" id="img1" exif="true" />

JS:

console.log($("#img1").exifPretty());

This only returns an empty set.

I also use the FileReader JQuery Plugin.

When I use the load function I get a file which is an extension of the original File object.

on:
    load: function(e, file) { }

But how do I retrieve the EXIF meta data from it?

Upvotes: 5

Views: 16186

Answers (3)

chings228
chings228

Reputation: 1889

with help of exif.js , the following script can get the exif from file input

$('#imageupload').change(function(){
    var file = this.files[0];
    var reader = new FileReader();
    reader.onload = function(e) {

        var exif = EXIF.readFromBinaryFile(this.result);
        console.log(exif);


     }

     reader.readAsArrayBuffer(file);


});

Upvotes: 0

Thomas Praxl
Thomas Praxl

Reputation: 777

If you're getting EXIF undefined, then use var EXIF = require('./exif.js'); FTW.

I managed to get that beast working without magic tricks (quick&dirty trial&error result):

'use strict';
var EXIF = require('./exif.js');

$(function() {
    $('#fileinput').on('change', function(){
       var files = this.files,
           i=0;
        for(i=0; i<files.length;++i){
            previewImage(this.files[i]);
        }
    });
    function previewImage(file) {
        var gallery = $('#gallery'),
            thumb = null,
            img = null,
            reader= null;
        if(!file.type.match('image/*')){
            throw 'File type must be an image';
        }

        thumb = $('<div />',{class: 'thumbnail'}).appendTo(gallery);
        img = $('<img />');
        reader = new FileReader();
        reader.onload = function(e){
            img.prop('src',reader.result);
            // important for exif-js! Set src attribute after calling img.prop
            img.src = img.prop('src');
            img.appendTo(thumb);
            EXIF.getData(img, function() {
                console.log(EXIF.pretty(img));
            });
        };
        reader.readAsDataURL(file);
    }

});

Upvotes: 2

Michael
Michael

Reputation: 33297

This is the solution:

on:
    load: function(event, file) {
    // get image meta data
    var base64 = event.target.result.replace(/^.*?,/,'');
    var binary = atob(base64);
    var exif = EXIF.readFromBinaryFile(new BinaryFile(binary));
}

Upvotes: -2

Related Questions