-2

I implemented an EXIF button in a gallery to read EXIF data using the well-known exif.js script from here. My image loads into a img0 img node, and then I use:

function exif() {
  EXIF.getData(img0, function() {
   var model    = EXIF.getTag(this, "Model");
   var datetime = EXIF.getTag(this, "DateTimeOriginal");
   var fnumber  = EXIF.getTag(this, "FNumber");

   .... <print this.src, and the exif output>

  });
}

When I click a thumbnail, the large image appears fullscreen (in a modal, with the img0 img node on it with the corresponding img.src), with an EXIF button (top, second from left), which executes the above function exif() when onclick. It works fine. Then I close the image by clicking on it, click on another thumbnail, and a second large image opens (with an updated img.src). When I click the EXIF button again, I can see a different img.src, yet the same EXIF as the first one (same gps, same time, etc). Somehow exif.js keeps the old img.src, and does not update it. You can check the gallery working here.

How do I force exif.js to see the new img.src file? Is it using the img in the cache? Can I ask exif.js to read the file instead of the img node? This is not supported in the documentation.

EDIT: I read the exif.js code, and there is a function that checks if the img node has exif:

function imageHasData(img) {return !!(img.exifdata); }

This function is called just before getImageData:

if (!imageHasData(img)) { getImageData(img, callback); } else...

Therefore, for some reason, imageHasData(img) returns false the first time and true the second.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58
  • You have to update the variable `img0` somehow – webdeb Apr 18 '22 at 00:03
  • `img0` is the node. What I do is update its `img0.src`. Maybe I should remove and add again the `img0` node... Placing some console.log around the exif.js, I see that the function `getImageData` is executed only for the first image. – Luis A. Florit Apr 18 '22 at 00:05

1 Answers1

0

By reading the exif.js code I realized which the problem was (see the EDIT). The img node has the src property, but also a new exifdata one added by exif.js. When closing the image, I did a img.src = "", but I did not erase img.exifdata.

So, now when I close the image I do:

img.src = "";
delete img.exifdata;

Now the problem went away, and the new exif is properly loaded.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58
  • Good that you found a solution! But since you are mutating nodes manually you'll probably have more errors and unpredictable behaviour. So maybe you should rethink the design. – webdeb Apr 18 '22 at 00:53
  • 1
    Thanks for the suggestion. Yet, I am running my gallery for several years without problems. This just appeared now because I added the exif info. It was my first try with exif.js. If I found some new issue, I will report back. – Luis A. Florit Apr 18 '22 at 01:15