From 086fb70eb22a32e9a4789618f2158e3ec17ebac6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 18 Jan 2026 11:16:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E7=94=9F=E5=9B=BE=E6=A8=A1=E5=9E=8B=20aspectRatio=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 aspectRatio(如 4:3)转换为阿里云 API 需要的 size 格式(如 1365*1024) - 结合 imageSize(1K/2K/4K)确定基准分辨率 - 自动计算保持总像素数接近目标分辨率的宽高 - 确保宽高在阿里云 API 要求的 512-2048 范围内 --- lib/ai/providers/alibaba-image.ts | 68 +++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/ai/providers/alibaba-image.ts b/lib/ai/providers/alibaba-image.ts index 24ecf2d5b..eb4fc3b35 100644 --- a/lib/ai/providers/alibaba-image.ts +++ b/lib/ai/providers/alibaba-image.ts @@ -368,19 +368,69 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) { seed: options.imageParams?.seed, }; - // 处理分辨率 + // 处理分辨率:结合 aspectRatio(宽高比)和 imageSize(分辨率等级) + const aspectRatio = options.imageParams?.aspectRatio || "auto"; + const imageSize = options.imageParams?.imageSize || "2K"; + + // 根据分辨率等级确定基准像素 + const baseSizeMap: Record = { + "1K": 1024, + "2K": 1536, + "4K": 2048, + }; + const baseSize = baseSizeMap[imageSize] || 1536; + + // 根据宽高比计算实际分辨率 + // 阿里云 API 要求宽高在 512-2048 之间 + const calculateSize = (ratio: string, base: number): string => { + // 解析宽高比 + const ratioMap: Record = { + "auto": [1, 1], + "1:1": [1, 1], + "16:9": [16, 9], + "9:16": [9, 16], + "4:3": [4, 3], + "3:4": [3, 4], + "3:2": [3, 2], + "2:3": [2, 3], + "5:4": [5, 4], + "4:5": [4, 5], + "21:9": [21, 9], + }; + + const [w, h] = ratioMap[ratio] || [1, 1]; + + // 计算宽高,保持总像素接近 base*base + const totalPixels = base * base; + const aspectValue = w / h; + + // height = sqrt(totalPixels / aspectValue) + // width = height * aspectValue + let height = Math.round(Math.sqrt(totalPixels / aspectValue)); + let width = Math.round(height * aspectValue); + + // 确保宽高在 512-2048 范围内 + const clamp = (val: number) => Math.max(512, Math.min(2048, val)); + width = clamp(width); + height = clamp(height); + + // 确保是偶数(某些图像处理可能需要) + width = Math.round(width / 2) * 2; + height = Math.round(height / 2) * 2; + + return `${width}*${height}`; + }; + + // 如果用户直接指定了 size,使用用户指定的 if (options.imageParams?.size) { params.size = options.imageParams.size; - } else if (options.imageParams?.imageSize) { - // 从 imageSize (如 "2K") 转换为具体分辨率 - const sizeMap: Record = { - "1K": "1024*1024", - "2K": "1536*1536", - "4K": "2048*2048", - }; - params.size = sizeMap[options.imageParams.imageSize] || "1024*1024"; + } else { + // 否则根据 aspectRatio 和 imageSize 计算 + params.size = calculateSize(aspectRatio, baseSize); } + console.log(`[Alibaba Image] Calculated size: aspectRatio=${aspectRatio}, imageSize=${imageSize} => size=${params.size}`); + // 根据图片数量确定编辑模式 const editMode = imageUrls.length === 1 ? "单图编辑" : `多图融合 (${imageUrls.length}张)`; console.log("[Alibaba Image] 编辑模式:", editMode);