Bladeren bron

图片比例序号

xzc 6 dagen geleden
bovenliggende
commit
f1b7565b69
1 gewijzigde bestanden met toevoegingen van 20 en 2 verwijderingen
  1. 20 2
      yudao-ui-admin-vue3/src/components/DynamicReport/BatchUploadFile.vue

+ 20 - 2
yudao-ui-admin-vue3/src/components/DynamicReport/BatchUploadFile.vue

@@ -52,6 +52,23 @@ const handleTemplateImage = async () => {
   }
   handleCloseShowUploadDialog()
 }
+// 从 base64 获取图片原始尺寸,按最大宽度 180 等比缩放
+const getImageSize = (base64: string): Promise<{ width: number; height: number }> => {
+  return new Promise((resolve) => {
+    const img = new Image()
+    img.onload = () => {
+      const maxWidth = 180
+      const ratio = maxWidth / img.naturalWidth
+      resolve({
+        width: maxWidth,
+        height: Math.round(img.naturalHeight * ratio)
+      })
+    }
+    img.onerror = () => resolve({ width: 180, height: 150 })
+    img.src = base64
+  })
+}
+
 const handleApply = async (file) => {
   // file.url 可能是已处理过的完整 URL(来自 CustomUploadFile 单独"应用"),也可能是原始路径(来自"一键应用")
   const isFullUrl = /^https?:\/\//.test(file.url)
@@ -75,13 +92,14 @@ const handleApply = async (file) => {
   if (!path) {
     path = props.colList[0]?.colCode
   }
+  const { width, height } = await getImageSize(base)
   const name = {
     url: filePath,
     path: path,
     x: 100,
     y: 50,
-    width: 180,
-    height: 150
+    width,
+    height
   }
   emit("uploadImg", {base, name});
   // 删掉列表上的