15

I am trying to extract EXIF data from a image(jpeg) which has been dragged into the browser or has been selected via a html file input element.

I managed to preview the image within the browser using FileReader and FileReader.readAsDataURL as described here.

and I found a EXIF library which allows to extract the EXIF data of an image via javascript. But for me it only works if I use it with normal img tags which load their content over a URL.

I also found this question on StackOverflow where the accepted answer states that it is just not possible.

But I am pretty sure that it can be realized because 500px.com extracts the EXIF data immediately after a file is added for upload and before the upload has been finished.

Some ideas how it should be possible to extract the EXIF data from the base64 encoded image I get from the FileReader?

Cœur
  • 37,241
  • 25
  • 195
  • 267
alex
  • 4,922
  • 7
  • 37
  • 51
  • a newer solution [exif-js](https://github.com/jseidelin/exif-js), based on the same [EXIF_ Library](http://blog.nihilogic.dk/2008/05/reading-exif-data-with-javascript.html) from [http://www.nihilogic.dk/](http://www.nihilogic.dk/) – arty May 29 '14 at 12:18

4 Answers4

17

I finally found a client side solution for the problem:

  1. Read the file using the FileReader and the method .readAsBinaryString
  2. Then wrap that binary string into a BinaryFile object which is already included in the EXIF Library
  3. Finally call EXIF.readFromBinaryFile(binaryFileObject);

and its done :)

Valjas
  • 4,994
  • 1
  • 21
  • 32
alex
  • 4,922
  • 7
  • 37
  • 51
  • Trying to include binaryajax.js in my project causes an access denied error, so this is not a solution for me. – Obi Wan Oct 21 '13 at 15:13
  • 1
    The link to nihilogic.dk is dead. – Evan Plaice Mar 03 '15 at 05:03
  • 1
    Links to BinaryFile object and EXIF Library are also dead. – MDeuerlein May 29 '16 at 18:04
  • If you want to use exif-js, see my answer here: http://stackoverflow.com/questions/18981555/retrieve-exif-image-meta-data-from-html5-fileapi-loaded-image#answer-40076913 – it does the trick pretty simple. – Thomas Praxl Oct 17 '16 at 00:26
  • This code will work without creating an tag: `const fr = new FileReader();` `fr.onload = () => {` `const result = exif.readFromBinaryFile(fr.result);` `};` `fr.readAsArrayBuffer(file);` "file" is an instance of "File" class – grumd May 04 '23 at 17:56
4

jQuery-fileExif javascript library reads image exif data before upload.
GitHub link, example jsfiddle from the library.

var someCallback = function(exifObject) {

    $('#cameraModel').val(exifObject.Model);
    $('#lat').val(exifObject.GPSLatitude);
    $('#lng').val(exifObject.GPSLongitude);
    // Uncomment the line below to examine the
    // EXIF object in console to read other values
    //console.log(exifObject);

  }

      try {
        $('#file').change(function() {
            $(this).fileExif(someCallback);
        });
      }
      catch (e) {
        alert(e);
      }
Nasser Al-Wohaibi
  • 4,562
  • 2
  • 36
  • 28
3

Have a look at the code of the FxIF firefox extension. It reads exif data using only JavaScript. To read the file contents, you can use the FileReader API of modern browsers.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    I really want to avoid browser extensions. As i mentioned i am already using FileReady but I don't know how to extract the EXIF data from the data I get from FileReader – alex Apr 26 '12 at 22:04
0

When you get your File class instance via the file input, do this:

import exif from 'exif-js';

const fr = new FileReader();

fr.onload = () => {
  const result = exif.readFromBinaryFile(fr.result);
  //    ^ EXIF data will be here
};

fr.readAsArrayBuffer(file);
grumd
  • 91
  • 3