Prerequisites
On the device, of course, there is a dependency that takes a picture, that in fact there is a sensor that can determine the orientation.
Some cameras do not. And even if it has a sensor, there is no guarantee that EXIF will be embedded in the image. EXIF is not built-in for PNG, for example, and some upload JPEG images, where EXIF is deleted for reasons of privacy and / or size (for example, GPS coordinates, etc.). Photoshop will remove EXIF if saved using the Save For Web option.
Try using other EXIF components
There is also the possibility that the EXIF component you are using has an error. Try other EXIF readers to fix this:
(and there is a ton of others ).
Raw orientation detection
If you only have a raw image without any EXIF information (such as PNG, TIFF, BMP), you can at least determine the orientation of the bitmap itself by doing the following:
function isPortrait(img) { var w = img.naturalWidth || img.width, h = img.naturalHeight || img.height; return (h > w); }
Just pass the element of the loaded image to the function. It will return true if in the portrait or false if square or landscape.
If you only have base64 encoded uri data, just set this as the source directly in the image element:
var img = document.createElement('img'); img.onload = function() { alert(isPortrait(img)); } img.src = <data-uri-here>;
Auto discovery
To automatically determine the orientation based on the content is very difficult to do, and there have been many attempts to do this using professional and open source approaches. But they never work 100%.
You will need quite a few algorithms to detect and recognize shapes, lines, and other objects. Huge project!
However, if your subjects are mostly faces / faces, you can implement face recognition. If you do not find any in the first pass, turn 90 degrees and try again. If you get a match, you can use this as the basis for “good guessing” (see, for example, this library for detecting faces )