优化代码

This commit is contained in:
wuxw 2024-09-29 17:59:32 +08:00
parent b44e2b509e
commit bccd2d1dcb

View File

@ -79,17 +79,29 @@ function imageToBease64(that, imageUrl, callback) {
const canvas = res[0].node; const canvas = res[0].node;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
const image = canvas.createImage(); const image = canvas.createImage();
canvas.width = imgData.width;
canvas.height = imgData.height;
image.src = imgData.path; image.src = imgData.path;
image.onload = function() { image.onload = function() {
const canvasWidth = imgData.width; // 获取图片宽度 let h = imgData.height;
const canvasHeight = imgData.height; // 获取图片高度 // 默认按比例压缩
ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight); // 绘制图片 let w = imgData.width;
if(h > 1080 || w > 1080){
let _rate = 0;
if(h > w){
_rate = h/1080;
h = 1080;
w = Math.floor(w/_rate);
}else{
_rate = w/1080;
w = 1080;
h = Math.floor(h/_rate);
}
}
canvas.width = w;
canvas.height = h;
ctx.drawImage(image, 0, 0, w, h); // 绘制图片
// 设置水印样式 // 设置水印样式
addWaterMark(ctx, imgData.width, imgData.height); addWaterMark(ctx, w, h);
//压缩比例 //压缩比例
let quality = 0.3; let quality = 0.3;