记得我初入前端差不多一年,公司做了一个webapp,有上传头像功用,当时这个项目不是我在担任,测试的时刻发明苹果用户照相上传头像会翻转,当时几个前端的同砚捯饬了一下昼也没处置惩罚,效果题目转到我这里,另有半个小时放工;当时也是一脸懵逼,起首想到的是,这怎样推断它是不是翻转了呢?安卓没题目啊,有些苹果手机相册内里的图片也没题目啊,js能有这类功用推断吗?上网查资料,果不其然,有!那就是exfe.js,继承研讨,此时组长姐姐已买好晚饭,老板也来慰劳,末了弄到晚上9点多,算是处置惩罚了。
时隔两年,昨天又碰到这个题目,已脱离上家公司一年半了,现公司做一个万圣节运动,内里也是上传图片,合成鬼脸图,早早两天前已做好发给项目经理(我们这边是长途,少量几个开发者),晚上快放工时,项目经理发消息“ios图片翻转,处置惩罚一下,今晚要上线,加个班”,内心一万个草泥马奔驰而过,1是我忘了ios有这个题目,2是已发给你2天了,你快放工的时刻跟我说有题目,加个班!我晚上布置要推掉!照样无偿!没有慰劳餐,也没有歉意,照样很好意义的加个班,照样苦笑一声复兴,“好吧,下次提早测一下”,这回毕竟遇见过,处置惩罚起来比较快,7点多搞定。后续又有什么布置的题目,不是我的题目了,又是快9点了。
只是险些相似的场景,慨叹一下。
上代码
html部份
<input type="file" accept="image/*" id="uploadImage" capture="camera" onchange="selectFileImage(this);" /> <img alt="preview" src="" id="myImage"/>
exfe.js来读取图片信息,我们上传的图片内里是有许多信息的
//猎取照片方向角属性,用户扭转掌握 EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, 'Orientation'); console.log(Orientation); });
Orientation的值有1,3,6,8之类的,离别代表0°,180°,顺时针90°,逆时针90°
当我们知道了图片的扭转角度,我们就能够用canvas来处置惩罚他们了,末了获得我们想要的效果,这里摘抄了网上一段代码,假如有特别功用,那就要本身写一些逻辑了,也就是推断角度,推断操纵系统,然后用canvas从新绘制,生成base64,末了对dom的操纵,显现图片。
上代码
function selectFileImage(fileObj) { var file = fileObj.files['0']; //图片方向角 var Orientation = null; if (file) { //猎取照片方向角属性,用户扭转掌握 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,都须要举行扭转 if(Orientation != "" && Orientation != 1){ alert('扭转处置惩罚'); switch(Orientation){ case 6://须要顺时针(向左)90度扭转 rotateImg(this,'left',canvas); break; case 8://须要逆时针(向右)90度扭转 rotateImg(this,'right',canvas); break; case 3://须要180度扭转 rotateImg(this,'right',canvas);//转两次 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度扭转 rotateImg(this,'left',canvas); break; case 8://须要逆时针(向右)90度扭转 rotateImg(this,'right',canvas); break; case 3://须要180度扭转 rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 1); } $("#myImage").attr("src", base64); }; }; oReader.readAsDataURL(file); } } //对图片扭转处置惩罚 function rotateImg(img, direction,canvas) { //最小与最大扭转方向,图片扭转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++; //扭转到原位置,即凌驾最大值 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; } }
以上就是用exfe.js和canvas处置惩罚挪动端 IOS 照相上传图片翻转题目(附代码)的细致内容,更多请关注ki4网别的相干文章!