From f3683bcc84617ca46ce83ebaca1ac182125521bc Mon Sep 17 00:00:00 2001 From: kuaifan Date: Wed, 16 Apr 2025 13:00:36 +0800 Subject: [PATCH] no message --- resources/assets/js/functions/common.js | 69 ++++++++++++++++++- .../pages/manage/components/DialogWrapper.vue | 27 +------- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/resources/assets/js/functions/common.js b/resources/assets/js/functions/common.js index 862c2d34b..6176520c5 100755 --- a/resources/assets/js/functions/common.js +++ b/resources/assets/js/functions/common.js @@ -1409,7 +1409,74 @@ const timezone = require("dayjs/plugin/timezone"); // 直接返回clear函数 return clear; - } + }, + + /** + * 通过URL生成base64图片 + * @param url 图片URL + * @param quality 图片质量,默认0.8 + * @param maxWidth 最大宽度,0表示不限制 + * @param maxHeight 最大高度,0表示不限制 + * @returns {Promise} + */ + generateBase64Image(url, quality = 1, maxWidth = 0, maxHeight = 0) { + return new Promise(resolve => { + let canvas = document.createElement('canvas'), + ctx = canvas.getContext('2d'), + img = new Image; + img.crossOrigin = 'Anonymous'; + img.onload = () => { + let width = img.width; + let height = img.height; + + // 处理等比例缩放 + if ((maxWidth > 0 || maxHeight > 0) && (width > 0 && height > 0)) { + // 计算宽高比 + const ratio = width / height; + + if (maxWidth > 0 && maxHeight > 0) { + // 同时指定了最大宽度和高度,按照最小比例缩放 + if (width > maxWidth || height > maxHeight) { + const ratioWidth = maxWidth / width; + const ratioHeight = maxHeight / height; + const ratioMin = Math.min(ratioWidth, ratioHeight); + + width = Math.round(width * ratioMin); + height = Math.round(height * ratioMin); + } + } else if (maxWidth > 0 && width > maxWidth) { + // 只指定了最大宽度 + width = maxWidth; + height = Math.round(width / ratio); + } else if (maxHeight > 0 && height > maxHeight) { + // 只指定了最大高度 + height = maxHeight; + width = Math.round(height * ratio); + } + } + + canvas.width = width; + canvas.height = height; + ctx.drawImage(img, 0, 0, width, height); + + let format = "png"; + if ($A.rightExists(url, "jpg") || $A.rightExists(url, "jpeg")) { + format = "jpeg" + } else if ($A.rightExists(url, "webp")) { + format = "webp" + } else if ($A.rightExists(url, "git")) { + format = "git" + } + resolve(canvas.toDataURL(`image/${format}`, quality)); + + // 清理资源 + canvas = null; + img = null; + ctx = null; + }; + img.src = url; + }) + }, }); /** diff --git a/resources/assets/js/pages/manage/components/DialogWrapper.vue b/resources/assets/js/pages/manage/components/DialogWrapper.vue index e7fa1800a..8724b79fa 100644 --- a/resources/assets/js/pages/manage/components/DialogWrapper.vue +++ b/resources/assets/js/pages/manage/components/DialogWrapper.vue @@ -3549,7 +3549,7 @@ export default { switch (type) { case 'image': if (this.$Electron) { - this.getBase64Image(value).then(base64 => { + $A.generateBase64Image(value).then(base64 => { this.$Electron.sendMessage('copyBase64Image', {base64}); }) } @@ -4242,31 +4242,6 @@ export default { }) }, - getBase64Image(url) { - return new Promise(resolve => { - let canvas = document.createElement('CANVAS'), - ctx = canvas.getContext('2d'), - img = new Image; - img.crossOrigin = 'Anonymous'; - img.onload = () => { - canvas.height = img.height; - canvas.width = img.width; - ctx.drawImage(img, 0, 0); - let format = "png"; - if ($A.rightExists(url, "jpg") || $A.rightExists(url, "jpeg")) { - format = "jpeg" - } else if ($A.rightExists(url, "webp")) { - format = "webp" - } else if ($A.rightExists(url, "git")) { - format = "git" - } - resolve(canvas.toDataURL(`image/${format}`)); - canvas = null; - }; - img.src = url; - }) - }, - getSelectedTextInElement(element) { const selection = document.getSelection(); if (selection.rangeCount > 0) {