前言
記得16年的時候我初入前端差不多一年,公司做了一個webapp,有上傳頭像功能,當時這個項目不是我在負責,測試的時候發(fā)現(xiàn)蘋果用戶拍照上傳頭像會翻轉(zhuǎn),當時幾個前端的同學捯飭了一下午也沒解決,結(jié)果問題轉(zhuǎn)到我這里,還有半個小時下班;當時也是一臉懵逼,首先想到的是,這怎么判斷它是否翻轉(zhuǎn)了呢?安卓沒問題啊,有些蘋果手機相冊里面的圖片也沒問題啊,js能有這種功能判斷嗎?上網(wǎng)查資料,果不其然,有!那就是exfe.js,繼續(xù)研究,此時組長姐姐已經(jīng)買好晚餐,老板也來慰問,最后弄到晚上9點多,算是解決了。
時隔兩年,昨天又遇到這個問題,已經(jīng)離開上家公司一年半了,現(xiàn)公司做一個萬圣節(jié)活動,里面也是上傳圖片,合成鬼臉圖,早早兩天前已經(jīng)做好發(fā)給項目經(jīng)理(我們這邊是遠程,少許幾個開發(fā)者),晚上快下班時,項目經(jīng)理發(fā)消息“ios圖片翻轉(zhuǎn),解決一下,今晚要上線,加個班”,心里一萬個草泥馬奔騰而過,1是我忘了ios有這個問題,2是已經(jīng)發(fā)給你2天了,你快下班的時候跟我說有問題,加個班!我晚上安排要推掉!還是無償!沒有慰問餐,也沒有歉意,還是很好意思的加個班,還是苦笑一聲回復,“好吧,下次提前測一下”,這回畢竟遇見過,處理起來比較快,7點多搞定。后續(xù)又有什么部署的問題,不是我的問題了,又是快9點了。
只是幾乎類似的場景,感慨一下。
上代碼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°,順時針90°,逆時針90°
當我們知道了圖片的旋轉(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; //修復ios if (navigator.userAgent.match(/iphone/i)) { console.log("iphone"); //如果方向角不為1,都需要進行旋轉(zhuǎn) if(Orientation != "" && Orientation != 1){ alert("旋轉(zhuǎn)處理"); switch(Orientation){ case 6://需要順時針(向左)90度旋轉(zhuǎn) rotateImg(this,"left",canvas); break; case 8://需要逆時針(向右)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)) {// 修復android var encoder = new JPEGEncoder(); base64 = encoder.encode(ctx.getImageData(0, 0, expectWidth, expectHeight), 80); }else{ if(Orientation != "" && Orientation != 1){ switch(Orientation){ case 6://需要順時針(向左)90度旋轉(zhuǎn) rotateImg(this,"left",canvas); break; case 8://需要逆時針(向右)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元素隱藏后獲取,否則會出錯 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://systransis.cn/yun/98886.html
摘要:后續(xù)過了幾天,公司購置了幾臺全新的測試機,測試同學將系統(tǒng)在一臺三星的機子上一測,又發(fā)現(xiàn)新問題了選擇完圖片進行本地預覽時,發(fā)現(xiàn)圖片翻轉(zhuǎn)了但上傳后再展示又是正常的。 最近在處理移動端選擇圖片實時預覽并上傳時遇到一個問題:上傳前圖片預覽正常,但上傳到服務器上的圖片展示到頁面上時,有時會出現(xiàn)圖片翻轉(zhuǎn)的問題,一般是翻轉(zhuǎn) 90 度。后經(jīng)一翻研究找到了問題所在,特在此記錄一下。 問題描述 接上面提到...
摘要:決定自己寫一個移動端圖片上傳組件。允許多選,加上事件的回調(diào)函數(shù)。在的回調(diào)函數(shù)中,我們能通過拿到所選擇的文件,但是文件是無法展示在頁面上的,通常的做法是使用轉(zhuǎn)為然后展示在頁面上。 背景 前段時間項目重構(gòu),改成SSR的項目,但之前用的圖片選擇上傳組件不支持SSR(server-side-render)。遂進行了調(diào)研,發(fā)現(xiàn)很多的工具。但有的太大,有的使用麻煩,有的不滿足使用需求。決定自己寫一...
摘要:下上傳圖片被旋轉(zhuǎn)解決方法用既然是解決問題,那就簡單說一下,直接上代碼方式使用在上可以直接調(diào)用照相機拍照,豎拍出來的圖片都會變成橫圖思路獲取到照片拍攝的方向角,對非橫拍的照片使用的進行角度旋轉(zhuǎn)修正。 iOS下html上傳圖片被旋轉(zhuǎn) 解決方法用exif.js+canvas既然是解決問題,那就簡單說一下,直接上代碼! html方式使用在iOS上可以直接調(diào)用照相機拍照,豎拍出來的圖片都會變成橫...
閱讀 2955·2023-04-25 19:20
閱讀 819·2021-11-24 09:38
閱讀 2075·2021-09-26 09:55
閱讀 2447·2021-09-02 15:11
閱讀 2087·2019-08-30 15:55
閱讀 3625·2019-08-30 15:54
閱讀 3164·2019-08-30 14:03
閱讀 2975·2019-08-29 17:11