Vue 前端等比例图片压缩

前端压缩图片的方式是利用canvas进行压缩 代码如下

 maximg='xxx' 最长边的值
getBase64(url, callback) {
            let _that = this
            var Img = new Image(),
                dataURL = '';
            Img.src = url + "?v=" + Math.random();
            Img.setAttribute("crossOrigin", 'Anonymous')
            Img.onload = function () {
                var canvas = document.createElement("canvas"),
                    width = Img.width,
                    height = Img.height;
                if (Math.max(width, height) > _that.maximg) {
                    if (width > height) {
                        canvas.width = _that.maximg;
                        canvas.height = _that.maximg * height / width
                    } else {
                        canvas.height = _that.maximg
                        canvas.width = _that.maximg * width / height
                    }
                } else {
                    canvas.width = width;
                    canvas.height = height;

                }

                canvas.getContext("2d").drawImage(Img, 0, 0, canvas.width, canvas.height);
                dataURL = canvas.toDataURL('image/jpeg');
                callback ? callback(dataURL) : null;
            };
        },
    this.toBase64('服务器资源url’);//返回的值是base64

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值