I am loading the extension code to show the photo from the android camera in html, as usual, on some img tag with the correct rotaion, especially for the img tag, whose width is wider than the height. I know this code is ugly, but you do not need to install any other packages. (I used the code above to get the exif rotation value, thanks.)
function getOrientation(file, callback) { var reader = new FileReader(); reader.onload = function(e) { var view = new DataView(e.target.result); if (view.getUint16(0, false) != 0xFFD8) return callback(-2); var length = view.byteLength, offset = 2; while (offset < length) { var marker = view.getUint16(offset, false); offset += 2; if (marker == 0xFFE1) { if (view.getUint32(offset += 2, false) != 0x45786966) return callback(-1); var little = view.getUint16(offset += 6, false) == 0x4949; offset += view.getUint32(offset + 4, little); var tags = view.getUint16(offset, little); offset += 2; for (var i = 0; i < tags; i++) if (view.getUint16(offset + (i * 12), little) == 0x0112) return callback(view.getUint16(offset + (i * 12) + 8, little)); } else if ((marker & 0xFF00) != 0xFF00) break; else offset += view.getUint16(offset, false); } return callback(-1); }; reader.readAsArrayBuffer(file); } var isChanged = false; function rotate(elem, orientation) { if (isIPhone()) return; var degree = 0; switch (orientation) { case 1: degree = 0; break; case 2: degree = 0; break; case 3: degree = 180; break; case 4: degree = 180; break; case 5: degree = 90; break; case 6: degree = 90; break; case 7: degree = 270; break; case 8: degree = 270; break; } $(elem).css('transform', 'rotate('+ degree +'deg)') if(degree == 90 || degree == 270) { if (!isChanged) { changeWidthAndHeight(elem) isChanged = true } } else if ($(elem).css('height') > $(elem).css('width')) { if (!isChanged) { changeWidthAndHeightWithOutMargin(elem) isChanged = true } else if(degree == 180 || degree == 0) { changeWidthAndHeightWithOutMargin(elem) if (!isChanged) isChanged = true else isChanged = false } } } function changeWidthAndHeight(elem){ var e = $(elem) var width = e.css('width') var height = e.css('height') e.css('width', height) e.css('height', width) e.css('margin-top', ((getPxInt(height) - getPxInt(width))/2).toString() + 'px') e.css('margin-left', ((getPxInt(width) - getPxInt(height))/2).toString() + 'px') } function changeWidthAndHeightWithOutMargin(elem){ var e = $(elem) var width = e.css('width') var height = e.css('height') e.css('width', height) e.css('height', width) e.css('margin-top', '0') e.css('margin-left', '0') } function getPxInt(pxValue) { return parseInt(pxValue.trim("px")) } function isIPhone(){ return ( (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) ); }
and then use for example
$("#banner-img").change(function () { var reader = new FileReader(); getOrientation(this.files[0], function(orientation) { rotate($('#banner-img-preview'), orientation, 1) }); reader.onload = function (e) { $('#banner-img-preview').attr('src', e.target.result) $('#banner-img-preview').css('display', 'inherit') };
Wonhyuk Cho Apr 04 '17 at 8:30 2017-04-04 08:30
source share