在移動端的頁面需要做用戶拍照上傳的功能時會有用,蘋果即使豎着拍照,上傳到網頁后它也會變成橫的,好像IOS得一個BUG,安卓就沒有這個問題。
要解決這個問題需要引入exif.js這個庫,網上隨便搜一下就有的下載。
//對圖片旋轉處理 added by lzk function rotateImg(img, direction,canvas) { //alert(img); //最小與最大旋轉方向,圖片旋轉4次后回到原方向 var min_step = 0; var max_step = 3; //var img = document.getElementById(pid); if (img == null)return; //img的高度和寬度不能在img元素隱藏后獲取,否則會出錯 var height = img.height; var width = img.width; //var step = img.getAttribute('step'); var step = 2; if (step == null) { step = min_step; } if (direction == 'right') { step++; //旋轉到原位置,即超過最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋轉角度以弧度值為參數 var degree = step * 90 * Math.PI / 180; var ctx = canvas.getContext('2d'); switch (step) { case 0: canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0); break; case 1: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, 0, -height); break; case 2: canvas.width = width; canvas.height = height; ctx.rotate(degree); ctx.drawImage(img, -width, -height); break; case 3: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, -width, 0); break; } }
/** *旋轉iphone拍照變橫后的照片 * * @param {*} o 傳入當前的input type=file,一般在change事件里傳入this * @param {*} callback 回調函數,照片旋轉后執行的事件 * @returns */ function rotatePhoto(o,callback){ var file=o.files[0]; var Orientation = null; if (file) { console.log("正在上傳,請稍后..."); var rFilter = /^(image\/jpeg|image\/png)$/i; // 檢查圖片格式 if (!rFilter.test(file.type)) { //showMyTips("請選擇jpeg、png格式的圖片", false); return; } // var URL = URL || webkitURL; //獲取照片方向角屬性,用戶旋轉控制 EXIF.getData(file, function() { // alert(EXIF.pretty(this)); EXIF.getAllTags(this); //alert(EXIF.getTag(this, 'Orientation')); Orientation = EXIF.getTag(this, 'Orientation'); //return; }); var oReader = new FileReader(); oReader.onload = function(e) { //var blob = URL.createObjectURL(file); //_compress(blob, file, basePath); var image = new Image(); image.src = e.target.result; image.onload = function() { var expectWidth = this.naturalWidth; var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) { expectWidth = 800; expectHeight = expectWidth * this.naturalHeight / this.naturalWidth; } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) { expectHeight = 1200; expectWidth = expectHeight * this.naturalWidth / this.naturalHeight; } var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = expectWidth; canvas.height = expectHeight; ctx.drawImage(this, 0, 0, expectWidth, expectHeight); var u = navigator.userAgent; //修復ios if (u.match(/iphone/i)) { console.log('iphone'); //如果方向角不為1,都需要進行旋轉 added by lzk if(Orientation != "" && Orientation != 1){ console.log('旋轉處理'); switch(Orientation){ case 6://需要順時針(向左)90度旋轉 console.log('需要順時針(向左)90度旋轉'); rotateImg(this,'left',canvas); break; case 8://需要逆時針(向右)90度旋轉 console.log('需要逆時針(向右)90度旋轉'); rotateImg(this,'right',canvas); break; case 3://需要180度旋轉 console.log('需要180度旋轉'); rotateImg(this,'right',canvas);//轉兩次 rotateImg(this,'right',canvas); break; } } //base64 在外定義為全局變量 //下面base64為得到旋轉后的base64圖片 base64 = canvas.toDataURL("image/jpeg", 0.8); var type = 'jpeg'; var fixtype = function (type) { type = type.toLocaleLowerCase().replace(/jpg/i, 'jpeg'); var r = type.match(/png|jpeg|bmp|gif/)[0]; return 'image/' + r; }; base64 = base64.replace(fixtype(type), 'image/jpeg'); // saveFile(base64, '111') 此處是如果想要保存當前圖片到本地的話; //這里是把已經旋轉過的圖片路徑賦值到img中 callback(base64) } else if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) { //如果安卓收到ios拍攝的照片,可以按PC端方式判斷 callback(e.target.result) } else{ //修復PC端上上傳ios拍出來的圖片 if(Orientation != "" && Orientation != 1){ //alert('旋轉處理'); switch(Orientation){ case 6://需要順時針(向左)90度旋轉 console.log('需要順時針(向左)90度旋轉'); rotateImg(this,'left',canvas); break; case 8://需要逆時針(向右)90度旋轉 console.log('需要逆時針(向右)90度旋轉'); rotateImg(this,'right',canvas); break; case 3://需要180度旋轉 console.log('需要180度旋轉'); rotateImg(this,'right',canvas);//轉兩次 rotateImg(this,'right',canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 0.8); var type = 'jpeg'; var fixtype = function (type) { type = type.toLocaleLowerCase().replace(/jpg/i, 'jpeg'); var r = type.match(/png|jpeg|bmp|gif/)[0]; return 'image/' + r; }; base64 = base64.replace(fixtype(type), 'image/jpeg'); callback(base64) } }; }; oReader.readAsDataURL(file); } }
上面兩個是基礎函數,引入exif.js后,直接復制放進script標簽里就行,下面的是調用方法:
document.getElementById('upload').addEventListener('change',function(){ //這里是你的上傳按鈕 //這里定義一個函數,用來處理你原本上傳完照片后的事情 var callback = function(src){ //這個src就是旋轉后圖片的base64地址 //這里是上傳旋轉圖片后,再插入文檔進行預覽,這里的事件根據實際需求編寫 var img = new Image() img.src = src img.onload = function(){ $('.box).append(img) } } //這里就是旋轉照片 rotatePhoto(this,callback) })