微信小程序 利用canvas图片压缩

<canvas canvas-id="canvas" style="width:{{cWidth}}px;height:{{cHeight}}px;position: absolute;left:-1000px;top:-1000px;"></canvas>
wx.chooseImage({
            count: 9, // 默认9
            sizeType: ['compressed'], // 指定只能为压缩图,首先进行一次默认压缩
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
            success: photo => {
                console.log(photo);
                //将tempFilePaths[0]加入到images数组 作为展示
                _this.data.images.push(photo.tempFilePaths[0])
                _this.setData({
                    images: _this.data.images
                })
                //-----返回选定照片的本地文件路径列表,获取照片信息-----------
                photo.tempFilePaths.forEach(item => {
                    console.log(item);
                    wx.getImageInfo({
                        src: item,
                        success: res => {
                            //---------利用canvas压缩图片--------------
                            var ratio = 2;
                            var canvasWidth = res.width //图片原始长宽
                            var canvasHeight = res.height
                            while (canvasWidth > 750 || canvasHeight > 1200) { // 保证宽在750,高1200
                                canvasWidth = Math.trunc(res.width / ratio)
                                canvasHeight = Math.trunc(res.height / ratio)
                                ratio++;
                            }
                            _this.setData({
                                cWidth: canvasWidth,
                                cHeight: canvasHeight
                            })
                            //----------绘制图形并取出图片路径--------------
                            var ctx = wx.createCanvasContext('canvas')
                            ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
                            ctx.draw(false, setTimeout(function () {
                                wx.canvasToTempFilePath({
                                    canvasId: 'canvas',
                                    destWidth: canvasWidth,
                                    destHeight: canvasHeight,
                                    success: res => {
                                        let token = wx.getStorageSync('token') || ""
                                        console.log(res.tempFilePath, '最终图片路径') //最终图片路径
                                        let path = res.tempFilePath
                                        wx.uploadFile({
                                            url: '',//上传地址
                                            method: 'POST',
                                            header: {
                                                "token": token
                                            },
                                            filePath: res.tempFilePath,
                                            name: 'image',
                                            success(res) {
                                                console.log(res);
                                            }
                                        })
                                        return
                                        //base64 格式
                                        let arr = path.split('.');
                                        let list = {};
                                        //获取图片类型
                                        list.imageType = '.' + arr[arr.length - 1];
                                        //对图片进行base64 获取base64Code
                                        list.base64Code = wx
                                            .getFileSystemManager()
                                            .readFileSync(path, 'base64');
                                        console.log(list, _this.data.images)
                                        //将list加入到imageList数组中 作为后台接口参数
                                        _this.data.imageList.push(list)
                                    },
                                    fail: res => {
                                        console.log(res.errMsg)
                                    }
                                })
                            }, 1000))
                        }, //留一定的时间绘制canvas
                        fail: res => {
                            console.log(res.errMsg)
                        }
                    })
                })

            }
        })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值