<canvas canvas-id="photo_canvas" style="width:{{canvasWidth}}px;height:{{canvasHeight}}px;"> </canvas>
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
success: function (photo) {
wx.getImageInfo({
src: photo.tempFilePaths[0],
success: function (res) {
var ctx = wx.createCanvasContext('photo_canvas');
console.log(res.height)
console.log(res.width)
// 设置目标图片大小
var towidth = Math.trunc(res.width / 10);
var toheight = Math.trunc(res.height / 10);
// 设置 canvas大小
_this.setData({
canvasWidth: towidth,
canvasHeight: toheight
})
// 压缩并画图
ctx.drawImage(photo.tempFilePaths[0], 0, 0, res.width, res.height,0,0,towidth,toheight)
//画图完成时获取图片tmp地址并上传
ctx.draw(false,function(){
wx.canvasToTempFilePath({
canvasId: 'photo_canvas',
success: function (res) {
wx.uploadFile({
url: 'http://',
filePath: res.tempFilePath,
name: 'img',
success: function (res) {
console.log(res)
}
})
}
}, this)
})