12

I'm reading an image file and rescaling it to send it to a server. In order to properly rescale it I need to change the orientation, since some pictures are taken with a mobile device camera. In order to do this rotation I did the following using the exif.js library:

EXIF.getData(img, function() {    
    alert(this.exifdata.Orientation);
    orientation = this.exifdata.Orientation;
    alert(orientation);
}

If this.exif.data.Orientation was, for example 6, the following output would be desierd: "6", "6".

This works when a file is uploaded via my computer. However, when uploaded from a mobile device camera, the output is "6" and then "0", no matter what value orientation had before.

Why does this happen?

Note: I'm only running on one thread. So the variable could not have been set externally during execution of these lines.

FredTheDino
  • 121
  • 2
  • Where is `orientation` declared? Is it a global variable or do you declare it implicit within that anonymous function? Maybe the later might be the problem. – Danmoreng Nov 09 '18 at 15:25
  • What happens if you set `orientation` before either of the alert statements? – SliceThePi Nov 09 '18 at 18:23

2 Answers2

6

The global variable window.orientation represents the orientation of the screen on mobile devices, and it's immutable. Changing the name of the variable- or putting var, let, or const before it to declare it locally- will fix your issue.

SliceThePi
  • 385
  • 2
  • 6
0

Exif support isn't exactly universal. Have you verified that the mobile device in question is properly baking in exif data in the first place?

The other orientation is the screen's current rotation, which would be the same regardless of the image you're looking at.