Prerequisites
There is of course a dependency here on the device that takes the picture that it actually has a sensor that can tell orientation.
Some cameras do others don't. And even if it does have a sensor there is no guarantee EXIF will be embedded in the image. EXIF is not embedded for PNG for example and some upload JPEG images where EXIF is removed for privacy and/or size reasons (eg. GPS coordinates etc.). Photoshop will remove EXIF if saved out using its "Save for web" option.
Try different EXIF components
There is also the possibility that the EXIF component you are using has a bug. Try other EXIF readers out there as well to eliminate this:
(and there are a ton of others).
Detecting "raw" orientation
In case you only have an "raw" image without any EXIF information (ie. PNG, TIFF, BMP) you can at least determine the orientation of the image bitmap itself by doing this:
function isPortrait(img) {
var w = img.naturalWidth || img.width,
h = img.naturalHeight || img.height;
return (h > w);
}
Just pass the uploaded image element to the function. It will return true if in portrait or false if square or landscape.
If you only have a base64 encoded data-uri just set that as source directly on an image element:
var img = document.createElement('img');
img.onload = function() {
alert(isPortrait(img));
}
img.src = <data-uri-here>;
Automatic detection
To automatically detect orientation based on content is a very hard thing to do and there has been many attempts doing this with professional approaches and open source approaches. But they never work 100%.
You will need quite a bit of algorithms to detect and recognize shapes, lines, other objects. A huge project!
However, if your subjects are mostly person/faces you can implement face detection. If you don't detect any at the first pass rotate 90 degrees and try again. If you get a match then you can use this as basis to do a "good guess" (see for example this library to do face detection)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…