300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 小程序上传服务器图片压缩 微信小程序压缩图片并上传到服务器(拿去即用)...

小程序上传服务器图片压缩 微信小程序压缩图片并上传到服务器(拿去即用)...

时间:2021-12-05 23:56:33

相关推荐

小程序上传服务器图片压缩 微信小程序压缩图片并上传到服务器(拿去即用)...

/**压缩图片*/compressionImage(tempFilePaths, params) {

let that= thiswx.getImageInfo({

src: tempFilePaths[0],

success:function(res) {var ctx = wx.createCanvasContext('photo_canvas');//设置canvas尺寸

var towidth = 500; //按宽度500px的比例压缩

var toheight = Math.trunc(500 * res.height /res.width);

that.setData({

canvas_h: toheight

})

ctx.drawImage(tempFilePaths[0], 0, 0, res.width, res.height, 0, 0, towidth, toheight)

that.createMap(ctx, params);

}

})

},/**创建画布并上传图片*/createMap(ctx, params) {

let that= this;

ctx.draw(true, function() {

wx.showLoading({

title:'压缩中',

})

setTimeout(()=>{

wx.canvasToTempFilePath({

canvasId:'photo_canvas',

fileType:"jpg",

success:function(res) {

wx.hideLoading();

wx.uploadFile({

url: app.globalData.baseUrl+ '/wechat/want/addWant',

filePath: res.tempFilePath,

name:'file',

formData: {'parameters': JSON.stringify(params)

},

success:function(res) {

console.log("state:" +JSON.parse(res.data).state)if (JSON.parse(res.data).state === 1) {

wx.showToast({

title:'发布成功',

duration:2000,

icon:"none",

success() {

setTimeout(function() {

wx.navigateBack({

delta:1,

})

},1000);

}

})

}

},

fail(res) {

console.log("fail" +res)

}

})

},

fail(res) {if (res.errMsg === "canvasToTempFilePath:fail:create bitmap failed") {

console.log("导出map失败")

}

}

},this)

},200);

})

},

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。