您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

移动端图片上传旋转、压缩问题的解决方案

2024/6/14 15:29:40发布45次查看
本篇文章就给大家带来移动端图片上传旋转、压缩问题的解决方案。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
前言
在手机上通过网页 input 标签拍照上传图片,有一些手机会出现图片旋转了90度d的问题,包括 iphone 和个别三星手机。这些手机竖着拍的时候才会出现这种问题,横拍出来的照片就正常显示。因此,可以通过获取手机拍照角度来对照片进行旋转,从而解决这个问题。
orientation
这个参数并不是所有图片都有的,不过手机拍出来的图片是带有这个参数的。
旋转角度参数值
0° 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>
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问javascript视频教程,jquery视频教程,bootstrap教程!
以上就是移动端图片上传旋转、压缩问题的解决方案的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product