新增功能
请问作者什么时候可以新增图片压缩功能
之前没考虑过这个问题。 您可以把需求说一下吗?合适的话我找时间加一下。
就是多图上传的时候可以压缩图片 默认限制500px 可以自定义
关于图片压缩,官方给的接口只有一个quality的属性,您这里的500px是指宽高可自定义么?按照官方的接口说明没看到如何调整宽高的参数。这有什么建议吗? https://uniapp.dcloud.io/api/media/image?id=compressimage
要使用canvas进行压缩
附上压缩代码 compressImg( ratio = 2, limitNum = 100, ) { let that = this; wx.getImageInfo({ src: imagePathArr[i], success: res => { var canvasWidth = res.width; //图片原始长宽 var canvasHeight = res.height; // console.log("图片的基本信息", res); while (canvasWidth > limitNum || canvasHeight > limitNum) { // 保证宽高在400以内 canvasWidth = Math.trunc(res.width / ratio); canvasHeight = Math.trunc(res.height / ratio); ratio++; } that.cWidth = canvasWidth; that.cHeight = canvasHeight; //----------绘制图形并取出图片路径-------------- var ctx = wx.createCanvasContext("canvas", that); ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight); ctx.draw( false, setTimeout(() => { wx.canvasToTempFilePath( { canvasId: "canvas", destWidth: canvasWidth, destHeight: canvasHeight, success: res => { // console.log("最终图片路径", res.tempFilePath); let filePath = res.tempFilePath; uni.getFileSystemManager().readFile({ filePath, //选择图片返回的相对路径 encoding: "base64", //编码格式 success: async res => { //成功的回调 const params = { content: "data:image/jpeg;base64," + res.data }; const { data } = await that.$globalAPI.imgupload( params ); that.value[remoteUrlIndex] = data; completeImages++; if (that.showUploadProgress) { uni.showToast({ title: "上传进度:" + completeImages + "/" + imagePathArr.length, icon: "none", mask: false, duration: 1000 }); } resolve( "success to upload image:" + remoteUrlIndex ); }, fail: res => { reject("failt to upload image:" + remoteUrlIndex); } }); }, fail: res => { console.log("生成失败", res); } }, that ); }, 1000) //canvas绘制时间 ); }, fail: res => { console.log(res.errMsg); } }); },
好的,等空了我看看哈
好
目前我只能进行单图片压缩,多图片压缩是返回的图片都是一样的 不知道这个bug 如何解决