前言
記得16年的時(shí)候我初入前端差不多一年,公司做了一個(gè)webapp,有上傳頭像功能,當(dāng)時(shí)這個(gè)項(xiàng)目不是我在負(fù)責(zé),測試的時(shí)候發(fā)現(xiàn)蘋果用戶拍照上傳頭像會(huì)翻轉(zhuǎn),當(dāng)時(shí)幾個(gè)前端的同學(xué)捯飭了一下午也沒解決,結(jié)果問題轉(zhuǎn)到我這里,還有半個(gè)小時(shí)下班;當(dāng)時(shí)也是一臉懵逼,首先想到的是,這怎么判斷它是否翻轉(zhuǎn)了呢?安卓沒問題啊,有些蘋果手機(jī)相冊里面的圖片也沒問題啊,js能有這種功能判斷嗎?上網(wǎng)查資料,果不其然,有!那就是exfe.js,繼續(xù)研究,此時(shí)組長姐姐已經(jīng)買好晚餐,老板也來慰問,最后弄到晚上9點(diǎn)多,算是解決了。
時(shí)隔兩年,昨天又遇到這個(gè)問題,已經(jīng)離開上家公司一年半了,現(xiàn)公司做一個(gè)萬圣節(jié)活動(dòng),里面也是上傳圖片,合成鬼臉圖,早早兩天前已經(jīng)做好發(fā)給項(xiàng)目經(jīng)理(我們這邊是遠(yuǎn)程,少許幾個(gè)開發(fā)者),晚上快下班時(shí),項(xiàng)目經(jīng)理發(fā)消息“ios圖片翻轉(zhuǎn),解決一下,今晚要上線,加個(gè)班”,心里一萬個(gè)草泥馬奔騰而過,1是我忘了ios有這個(gè)問題,2是已經(jīng)發(fā)給你2天了,你快下班的時(shí)候跟我說有問題,加個(gè)班!我晚上安排要推掉!還是無償!沒有慰問餐,也沒有歉意,還是很好意思的加個(gè)班,還是苦笑一聲回復(fù),“好吧,下次提前測一下”,這回畢竟遇見過,處理起來比較快,7點(diǎn)多搞定。后續(xù)又有什么部署的問題,不是我的問題了,又是快9點(diǎn)了。
只是幾乎類似的場景,感慨一下。
上代碼html部分
exfe.js來讀取圖片信息,我們上傳的圖片里面是有很多信息的
//獲取照片方向角屬性,用戶旋轉(zhuǎn)控制 EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, "Orientation"); console.log(Orientation); });
Orientation的值有1,3,6,8之類的,分別代表0°,180°,順時(shí)針90°,逆時(shí)針90°
當(dāng)我們知道了圖片的旋轉(zhuǎn)角度,我們就可以用canvas來處理他們了,最后得到我們想要的結(jié)果,這里摘抄了網(wǎng)上一段代碼,如果有特殊功能,那就要自己寫一些邏輯了,也就是判斷角度,判斷操作系統(tǒng),然后用canvas重新繪制,生成base64,最后對dom的操作,顯示圖片。
上代碼
function selectFileImage(fileObj) { var file = fileObj.files["0"]; //圖片方向角 var Orientation = null; if (file) { //獲取照片方向角屬性,用戶旋轉(zhuǎn)控制 EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, "Orientation"); console.log(Orientation) }); var oReader = new FileReader(); oReader.onload = function(e) { 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 base64 = null; //修復(fù)ios if (navigator.userAgent.match(/iphone/i)) { console.log("iphone"); //如果方向角不為1,都需要進(jìn)行旋轉(zhuǎn) if(Orientation != "" && Orientation != 1){ alert("旋轉(zhuǎn)處理"); switch(Orientation){ case 6://需要順時(shí)針(向左)90度旋轉(zhuǎn) rotateImg(this,"left",canvas); break; case 8://需要逆時(shí)針(向右)90度旋轉(zhuǎn) rotateImg(this,"right",canvas); break; case 3://需要180度旋轉(zhuǎn) rotateImg(this,"right",canvas);//轉(zhuǎn)兩次 rotateImg(this,"right",canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 1); }else if (navigator.userAgent.match(/Android/i)) {// 修復(fù)android var encoder = new JPEGEncoder(); base64 = encoder.encode(ctx.getImageData(0, 0, expectWidth, expectHeight), 80); }else{ if(Orientation != "" && Orientation != 1){ switch(Orientation){ case 6://需要順時(shí)針(向左)90度旋轉(zhuǎn) rotateImg(this,"left",canvas); break; case 8://需要逆時(shí)針(向右)90度旋轉(zhuǎn) rotateImg(this,"right",canvas); break; case 3://需要180度旋轉(zhuǎn) rotateImg(this,"right",canvas);//轉(zhuǎn)兩次 rotateImg(this,"right",canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 1); } $("#myImage").attr("src", base64); }; }; oReader.readAsDataURL(file); } } //對圖片旋轉(zhuǎn)處理 function rotateImg(img, direction,canvas) { //最小與最大旋轉(zhuǎn)方向,圖片旋轉(zhuǎn)4次后回到原方向 var min_step = 0; var max_step = 3; if (img == null)return; //img的高度和寬度不能在img元素隱藏后獲取,否則會(huì)出錯(cuò) 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++; //旋轉(zhuǎn)到原位置,即超過最大值 step > max_step && (step = min_step); } else { step--; step < min_step && (step = max_step); } //旋轉(zhuǎn)角度以弧度值為參數(shù) 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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/98886.html
摘要:后續(xù)過了幾天,公司購置了幾臺(tái)全新的測試機(jī),測試同學(xué)將系統(tǒng)在一臺(tái)三星的機(jī)子上一測,又發(fā)現(xiàn)新問題了選擇完圖片進(jìn)行本地預(yù)覽時(shí),發(fā)現(xiàn)圖片翻轉(zhuǎn)了但上傳后再展示又是正常的。 最近在處理移動(dòng)端選擇圖片實(shí)時(shí)預(yù)覽并上傳時(shí)遇到一個(gè)問題:上傳前圖片預(yù)覽正常,但上傳到服務(wù)器上的圖片展示到頁面上時(shí),有時(shí)會(huì)出現(xiàn)圖片翻轉(zhuǎn)的問題,一般是翻轉(zhuǎn) 90 度。后經(jīng)一翻研究找到了問題所在,特在此記錄一下。 問題描述 接上面提到...
摘要:決定自己寫一個(gè)移動(dòng)端圖片上傳組件。允許多選,加上事件的回調(diào)函數(shù)。在的回調(diào)函數(shù)中,我們能通過拿到所選擇的文件,但是文件是無法展示在頁面上的,通常的做法是使用轉(zhuǎn)為然后展示在頁面上。 背景 前段時(shí)間項(xiàng)目重構(gòu),改成SSR的項(xiàng)目,但之前用的圖片選擇上傳組件不支持SSR(server-side-render)。遂進(jìn)行了調(diào)研,發(fā)現(xiàn)很多的工具。但有的太大,有的使用麻煩,有的不滿足使用需求。決定自己寫一...
摘要:下上傳圖片被旋轉(zhuǎn)解決方法用既然是解決問題,那就簡單說一下,直接上代碼方式使用在上可以直接調(diào)用照相機(jī)拍照,豎拍出來的圖片都會(huì)變成橫圖思路獲取到照片拍攝的方向角,對非橫拍的照片使用的進(jìn)行角度旋轉(zhuǎn)修正。 iOS下html上傳圖片被旋轉(zhuǎn) 解決方法用exif.js+canvas既然是解決問題,那就簡單說一下,直接上代碼! html方式使用在iOS上可以直接調(diào)用照相機(jī)拍照,豎拍出來的圖片都會(huì)變成橫...
閱讀 3018·2023-04-25 19:20
閱讀 882·2021-11-24 09:38
閱讀 2163·2021-09-26 09:55
閱讀 2514·2021-09-02 15:11
閱讀 2268·2019-08-30 15:55
閱讀 3672·2019-08-30 15:54
閱讀 3224·2019-08-30 14:03
閱讀 3026·2019-08-29 17:11