Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 482
confile Avatar asked Nov 23 '22 14:11

confile


1 Answers

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);
    }

});
like image 139
Thomas Praxl Avatar answered Apr 07 '23 16:04

Thomas Praxl