这一版本优化了很多
This commit is contained in:
68
unpackage/dist/dev/mp-weixin/utils/watermark.js
vendored
Normal file
68
unpackage/dist/dev/mp-weixin/utils/watermark.js
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
const common_vendor = require("../common/vendor.js");
|
||||
function addTimestampWatermark({ tempFilePath, canvasId = "watermarkCanvas", canvasWidthRef, canvasHeightRef, instance }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
common_vendor.index.getImageInfo({
|
||||
src: tempFilePath,
|
||||
success: (imageInfo) => {
|
||||
const imgWidth = imageInfo.width;
|
||||
const imgHeight = imageInfo.height;
|
||||
if (canvasWidthRef && canvasWidthRef.value !== void 0) {
|
||||
canvasWidthRef.value = imgWidth;
|
||||
}
|
||||
if (canvasHeightRef && canvasHeightRef.value !== void 0) {
|
||||
canvasHeightRef.value = imgHeight;
|
||||
}
|
||||
common_vendor.nextTick$1(() => {
|
||||
const ctx = common_vendor.index.createCanvasContext(canvasId, instance);
|
||||
if (!ctx) {
|
||||
reject("创建水印画布上下文失败");
|
||||
return;
|
||||
}
|
||||
ctx.drawImage(tempFilePath, 0, 0, imgWidth, imgHeight);
|
||||
const now = /* @__PURE__ */ new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
const hours = String(now.getHours()).padStart(2, "0");
|
||||
const minutes = String(now.getMinutes()).padStart(2, "0");
|
||||
const seconds = String(now.getSeconds()).padStart(2, "0");
|
||||
const timeStr = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
const fontSize = Math.max(14, Math.floor(imgWidth * (14 / 375)));
|
||||
ctx.setFontSize(fontSize);
|
||||
ctx.setFillStyle("#C9CBD4");
|
||||
ctx.setShadow(2, 2, 4, "rgba(0, 0, 0, 0.6)");
|
||||
const padding = fontSize;
|
||||
const textWidth = timeStr.length * (fontSize * 0.55);
|
||||
const x = imgWidth - textWidth - padding;
|
||||
const y = imgHeight - padding;
|
||||
ctx.fillText(timeStr, x, y);
|
||||
ctx.draw(false, () => {
|
||||
setTimeout(() => {
|
||||
common_vendor.index.canvasToTempFilePath({
|
||||
canvasId,
|
||||
destWidth: imgWidth,
|
||||
destHeight: imgHeight,
|
||||
fileType: "jpg",
|
||||
quality: 0.9,
|
||||
success: (exportRes) => {
|
||||
resolve(exportRes.tempFilePath);
|
||||
},
|
||||
fail: (err) => {
|
||||
common_vendor.index.__f__("error", "at utils/watermark.js:80", "导出带水印图片失败:", err);
|
||||
reject(err);
|
||||
}
|
||||
}, instance);
|
||||
}, 150);
|
||||
});
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
common_vendor.index.__f__("error", "at utils/watermark.js:89", "获取图片信息失败:", err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.addTimestampWatermark = addTimestampWatermark;
|
||||
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/watermark.js.map
|
||||
Reference in New Issue
Block a user