挪动端图片上传扭转、紧缩的解决方案

媒介

在手机上经由过程网页 input 标签照相上传图片,有一些手时机涌现图片扭转了90度d的题目,包括 iPhone 和一般三星手机。这些手机竖着拍的时刻才会涌现这类题目,横拍出来的照片就一般显现。因而,能够经由过程猎取手机照相角度来对照片举行扭转,从而处理这个题目。

Orientation

这个参数并非一切图片都有的,不过手机拍出来的图片是带有这个参数的。

扭转角度参数值
1
顺时针90°6
逆时针90°8
180°3

参数为 1 的时刻显现一般,那末在这些横拍显现一般,即 Orientation = 1 的手机上,竖拍的参数为 6。

想要猎取 Orientation 参数,能够经由过程 exif.js 库来操纵。exif.js 功用许多,体积也很大,未紧缩之前足足有 30k,这对手机页面加载来讲是异常大影响的。而我只须要猎取 Orientation 信息罢了,所以我这里删减了 exif.js 库的一些代码,将代码缩小到几KB。

exif.js 猎取 Orientation :

EXIF.getData(file, function() {  
    var Orientation = EXIF.getTag(this, 'Orientation');
});

file 则是 input 文件表单上传的文件。上传的文件经由 fileReader.readAsDataURL(file) 就能够完成预览图片了,这方面不清楚的能够检察:HTML5 进阶系列:文件上传下载

扭转

扭转须要用到 canvas 的 rotate() 要领。

ctx.rotate(angle);

rotate 要领的参数为扭转弧度。须要将角度转为弧度:degrees * Math.PI / 180

扭转的中心点默许都在 canvas 的出发点,即 ( 0, 0 )。扭转的道理以下图:

《挪动端图片上传扭转、紧缩的解决方案》

扭转以后,假如从 ( 0, 0 ) 点举行 drawImage(),那末画出来的位置就是在左图中的扭转 90 度后的位置,不在可视地区呢。扭转以后,坐标轴也随着扭转了,想要显如今可视地区呢,须要将 ( 0, 0 ) 点往 y 轴的反方向移 y 个单元,此时的起始点则为 ( 0, -y )。

同理,能够取得扭转 -90 度后的起始点为 ( -x, 0 ),扭转 180 度后的起始点为 ( -x, -y )。

紧缩

手机拍出来的照片太大,而且运用 base64 编码的照片会比原照片大,那末上传的时刻举行紧缩就异常有必要的。如今的手机像素这么高,拍出来的照片宽高都有几千像素,用 canvas 来衬着这照片的速率会相对比较慢。

因而第一步须要先对上传照片的宽高做限定,推断宽度或高度是不是超越哪一个局限,则等比紧缩其宽高。

var ratio = width / height;
if(imgWidth > imgHeight && imgWidth > xx){
    imgWidth = xx;
    imgHeight = Math.ceil(xx / ratio);
}else if(imgWidth < imgHeight && imgHeight > yy){
    imgWidth = Math.ceil(yy * ratio);
    imgHeight = yy;
}

第二步就经由过程 canvas.toDataURL() 要领来紧缩照片质量。

canvas.toDataURL("image/jpeg", 1);

toDataURL() 要领返回一个包括图片展现的 data URI 。运用两个参数,第一个参数为图片格式,默许为 image/png。第二个参数为紧缩质量,在指定图片格式为 image/jpeg 或 image/webp的情况下,能够从 0 到 1 的区间内挑选图片的质量。

总结

综合以上,例子的代码包括精简的exif.js库地点:file-demo

重要的中心代码以下:

<input type="file" id="files" >
<img src="blank.gif" id="preview">
<script src="small-exif.js"></script>
<script>
var ipt = document.getElementById('files'),
    img = document.getElementById('preview'),
    Orientation = null;
ipt.onchange = function () {
    var file = ipt.files[0],
        reader = new FileReader(),
        image = new Image();
        
    if(file){
        EXIF.getData(file, function() {  
            Orientation = EXIF.getTag(this, 'Orientation');
        });
        reader.onload = function (ev) {
            image.src = ev.target.result;
            image.onload = function () {
                var imgWidth = this.width,
                    imgHeight = this.height;
                // 掌握上传图片的宽高
                if(imgWidth > imgHeight && imgWidth > 750){
                    imgWidth = 750;
                    imgHeight = Math.ceil(750 * this.height / this.width);
                }else if(imgWidth < imgHeight && imgHeight > 1334){
                    imgWidth = Math.ceil(1334 * this.width / this.height);
                    imgHeight = 1334;
                }
                
                var canvas = document.createElement("canvas"),
                ctx = canvas.getContext('2d');
                canvas.width = imgWidth;
                canvas.height = imgHeight;
                if(Orientation && Orientation != 1){
                    switch(Orientation){
                        case 6:     // 扭转90度
                            canvas.width = imgHeight;    
                            canvas.height = imgWidth;    
                            ctx.rotate(Math.PI / 2);
                            // (0,-imgHeight) 从扭转道理图那边取得的起始点
                            ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight);
                            break;
                        case 3:     // 扭转180度
                            ctx.rotate(Math.PI);    
                            ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight);
                            break;
                        case 8:     // 扭转-90度
                            canvas.width = imgHeight;    
                            canvas.height = imgWidth;    
                            ctx.rotate(3 * Math.PI / 2);    
                            ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight);
                            break;
                    }
                }else{
                    ctx.drawImage(this, 0, 0, imgWidth, imgHeight);
                }
                img.src = canvas.toDataURL("image/jpeg", 0.8);
            }
        }
        reader.readAsDataURL(file);
    }
}
</script>

更多文章:lin-xin/blog

微信赞扬

《挪动端图片上传扭转、紧缩的解决方案》

    原文作者:linshuai
    原文地址: https://segmentfault.com/a/1190000009990033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞