博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
上传图片,JS中等比压缩返回BASE64格式String
阅读量:4667 次
发布时间:2019-06-09

本文共 3504 字,大约阅读时间需要 11 分钟。

微信公众号里,需要上传照片,由于手机直接拍照上传,照片比较大,直接转BASE64上传会很慢

所以最好在JS中压缩后,再上传

网上找了个方法,照搬试了一下,可以用,但是,图片会被裁剪掉一大块

于是改了改,现在会根据图片是纵向还是横向等比例的进行压缩

哦!对了,这样转出来的BASE64字符串会有23位的文件头

如果后台用JDK的BASE64Decoder再转成图片的话,需要先把文件头去掉,不然无法转换的

 

HTML部分

                                                    

 

Js部分

    function start_upload(obj){              if(!isCanvasSupported){                  console.log("画布不存在,压缩失败");               }else{                  compress(event, function(base64Img){                    document.getElementById(obj.name+"Base64").value=base64Img;                 });               }          }                //判断是否存在画布          function isCanvasSupported() {              var elem = document.createElement('canvas');              return !!(elem.getContext && elem.getContext('2d'));          }                    //压缩方法          function compress(event, callback) {              if ( typeof (FileReader) === 'undefined') {                  console.log("当前浏览器内核不支持base64图标压缩");              } else {                  try {                      var file = event.currentTarget.files[0];                       if(!/image\/\w+/.test(file.type)){                                 alert("请确保文件为图像类型");                                                        return false;                         }                       var reader = new FileReader();                      reader.onload = function (e) {                      var image = $('');                      image.load(function () {                      console.log("开始压缩");                    //压缩的像素大小,square越大,像素越高                      var square = 500;                      var canvas = document.createElement('canvas');                    if (this.width > this.height) {                                               canvas.width = Math.round(square * this.width / this.height);                        canvas.height = square;                     } else {                          canvas.width = square;                        canvas.height = Math.round(square * this.height / this.width);                                        }                        var context = canvas.getContext('2d');                      context.clearRect(0, 0, canvas.width, canvas.height);                      var imageWidth;                      var imageHeight;                      var offsetX = 0;                      var offsetY = 0;                      console.log(this.width);                    console.log(this.height);                    if (this.width > this.height) {                                               imageWidth = Math.round(square * this.width / this.height);                        imageHeight = square;                     } else {                          imageWidth = square;                      imageHeight = Math.round(square * this.height / this.width);                      }                     console.log(imageWidth);                    console.log(imageHeight);                    context.drawImage(this, 0, 0, imageWidth, imageHeight);                      var data = canvas.toDataURL('image/jpeg');                          //压缩完成执行回调                         callback(data);                      });                      image.attr('src', e.target.result);                      };                      reader.readAsDataURL(file);                  } catch(e) {                      console.log("压缩失败!");                  }              }          }

 

posted on
2018-04-11 16:46 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mide0131/p/8796696.html

你可能感兴趣的文章
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
win7,Ubuntu 12.04 双系统修改启动项顺序三方法
查看>>
python--列表推导式和生成表达式
查看>>
P - Psychos in a Line 单调队列
查看>>
POJ 2653 Pick-up sticks(计算几何)
查看>>
大型网站高并发的架构演变图-摘自网络
查看>>
8丶运行及总结
查看>>
Unity获取手机的电量时间
查看>>
Spring框架:Spring容器具体解释
查看>>
MongoDB 3.2 从安装到使用。
查看>>
sqlplus登录、连接命令
查看>>
C#简单线程同步例子
查看>>
VC++与MySQL数据库的连接(Window)
查看>>
将查询列表内容保存到excel表格中,并保存到相应的盘中
查看>>
python requests提示警告InsecureRequestWarning
查看>>