From f46ee10eba6bfcf71ba69bfbe4b1e531f28aa45a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 18 Jan 2026 11:19:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E7=94=9F=E5=9B=BE=E5=88=86=E8=BE=A8=E7=8E=87=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=AE=BD=E9=AB=98=E6=AF=94=E5=92=8C?= =?UTF-8?q?=E5=88=86=E8=BE=A8=E7=8E=87=E6=AD=A3=E7=A1=AE=E9=85=8D=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改进 clamp 逻辑:超出范围时按比例缩放而非直接截断 - 确保在缩放后仍保持正确的宽高比 - 分辨率等级(1K/2K/4K)和宽高比(4:3/16:9等)可以正确组合 --- lib/ai/providers/alibaba-image.ts | 35 ++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/ai/providers/alibaba-image.ts b/lib/ai/providers/alibaba-image.ts index eb4fc3b..a59904d 100644 --- a/lib/ai/providers/alibaba-image.ts +++ b/lib/ai/providers/alibaba-image.ts @@ -399,20 +399,45 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) { }; const [w, h] = ratioMap[ratio] || [1, 1]; + const aspectValue = w / h; // 计算宽高,保持总像素接近 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); + // 阿里云 API 限制:宽高在 512-2048 之间 + const MIN_SIZE = 512; + const MAX_SIZE = 2048; + + // 如果超出范围,按比例缩放,同时保持宽高比 + if (width > MAX_SIZE) { + const scale = MAX_SIZE / width; + width = MAX_SIZE; + height = Math.round(height * scale); + } + if (height > MAX_SIZE) { + const scale = MAX_SIZE / height; + height = MAX_SIZE; + width = Math.round(width * scale); + } + if (width < MIN_SIZE) { + const scale = MIN_SIZE / width; + width = MIN_SIZE; + height = Math.round(height * scale); + } + if (height < MIN_SIZE) { + const scale = MIN_SIZE / height; + height = MIN_SIZE; + width = Math.round(width * scale); + } + + // 最终 clamp 确保在范围内 + width = Math.max(MIN_SIZE, Math.min(MAX_SIZE, width)); + height = Math.max(MIN_SIZE, Math.min(MAX_SIZE, height)); // 确保是偶数(某些图像处理可能需要) width = Math.round(width / 2) * 2;