WeChat Mini Program Mini Program Image Compression

js inside
var requestCallback = function (err, data) {//Request result
wx.getImageInfo({//Get this image image compression
src: files[filescount],//I don't need to say the selection of pictures and pictures that need to be obtained
success: function (res) {
var ctx = wx.createCanvasContext('photo_canvas');//Use a canvas
var canvasWidth = res.width//Original image width 
var canvasHeight = res.height;//Original image height
var tWidth = 800; //Set the initial width of the thumbnail //Adjustable
var tHeight = 640; //Set the initial height of the thumbnail //adjustable
if (canvasWidth > tWidth || canvasHeight > tHeight) {
// Calculate the width and height of the thumbnail in proportion
if (canvasWidth > canvasHeight * 1.8) {
tHeight = Math.floor(parseFloat(canvasHeight) * (parseFloat(tWidth) / parseFloat(canvasWidth)));
}
else {
tWidth = Math.floor(parseFloat(canvasWidth) * (parseFloat(tHeight) / parseFloat(canvasHeight)));
}
}
else {
tWidth = canvasWidth;
tHeight = canvasHeight;
}
// draw new graph
ctx.drawImage(files[filescount], 0, 0, tWidth, tHeight)
ctx.draw()
//Download canvas image
setTimeout(function () {
wx.canvasToTempFilePath({
canvasId: 'photo_canvas',
success: function (res) {
var filePath = res.tempFilePath
var oldfilePath = files[filescount] //The following is my call to Tencent cloud storage object business does not need to be deleted
var Key = oldfilePath.substr(oldfilePath.lastIndexOf('/') + 1);
var yskeyCl = Key.split ('.')
var yskey = yskeyCl [2] + '-ys.' + yskeyCl [3]
cos.postObject ({
Bucket: config.Bucket,
Region: config.Region,
Key: yskey,
FilePath: filePath
}, function (err, data) {
JiaruFile(yskeyCl[2] + '.' + yskeyCl[3])//Add to the database
filescount = filescount + 1
zhixing()//This is the concurrency control that handles itself according to the business
});
},
fail: function (error) {
console.log(error)
}
})
}, 100)
}
})

}

页面要加上这个哦

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571683&siteId=291194637