From 77c4d1e7cccbf71f62b15e7b214ea7c2ca62c610 Mon Sep 17 00:00:00 2001 From: dlh <2773843782@qq.com> Date: Fri, 14 Aug 2026 20:45:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(media):=20Grok=20=E5=9B=BE=E7=89=87=20-=20?= =?UTF-8?q?=E8=A1=A5=E9=BD=90=E5=83=8F=E7=B4=A0=E5=B0=BA=E5=AF=B8=E7=9A=84?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AE=BD=E9=AB=98=E6=AF=94=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: dlh <2773843782@qq.com> Original-PR: https://github.com/ddcat-ai/open-ai-canvas/pull/223 --- backend/internal/service/provider.go | 11 +++++++++ backend/internal/service/provider_test.go | 30 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/backend/internal/service/provider.go b/backend/internal/service/provider.go index 5471093e1..06fda3c75 100644 --- a/backend/internal/service/provider.go +++ b/backend/internal/service/provider.go @@ -819,6 +819,17 @@ func normalizeGrokImageAspectRatio(size string) string { return "4:3" case h*3 == w*4 || (ratio > 0.7 && ratio < 0.85): return "3:4" + // 像素尺寸路径必须显式覆盖 2:3 / 3:2 / 1:2 / 2:1:冒号字符串能直达(见上方 switch), + // 但像素路径只靠 w>h 兜底会把 768x1152(2:3,ratio 0.667)错标成 9:16、 + // 1152x768(3:2,ratio 1.5)错标成 16:9,xAI 按错比例裁切生成图。 + case w*3 == h*2 || (ratio >= 0.6 && ratio < 0.72): + return "2:3" + case w*2 == h*3 || (ratio > 1.35 && ratio < 1.6): + return "3:2" + case h == w*2 || (ratio > 0.45 && ratio < 0.55): + return "1:2" + case w == h*2 || (ratio > 1.85 && ratio < 2.2): + return "2:1" case w > h: return "16:9" default: diff --git a/backend/internal/service/provider_test.go b/backend/internal/service/provider_test.go index 82086e85d..235a9fabc 100644 --- a/backend/internal/service/provider_test.go +++ b/backend/internal/service/provider_test.go @@ -252,6 +252,36 @@ func TestGrokImageRequestBodyMapsAspectRatio(t *testing.T) { } } +func TestNormalizeGrokImageAspectRatioPixelSizes(t *testing.T) { + // 像素尺寸路径必须覆盖 2:3 / 3:2 / 1:2 / 2:1:修复前 768x1152 会被 w>h 兜底错标成 9:16、 + // 1152x768 错标成 16:9,xAI 按错比例裁切生成图。 + cases := []struct { + size string + want string + }{ + {"768x1152", "2:3"}, // 1280x1920 同族:竖图 2:3 + {"1280x1920", "2:3"}, // 审查复现用例 + {"1152x768", "3:2"}, + {"1920x1280", "3:2"}, + {"540x1080", "1:2"}, + {"1080x540", "2:1"}, + {"1280x720", "16:9"}, + {"720x1280", "9:16"}, + {"800x800", "1:1"}, + } + for _, tc := range cases { + if got := normalizeGrokImageAspectRatio(tc.size); got != tc.want { + t.Errorf("normalizeGrokImageAspectRatio(%q) = %q, want %q", tc.size, got, tc.want) + } + } + // 冒号字符串路径回归:2:3 等比值字符串应原样透传。 + for _, size := range []string{"2:3", "3:2", "1:2", "2:1"} { + if got := normalizeGrokImageAspectRatio(size); got != size { + t.Errorf("normalizeGrokImageAspectRatio(%q) = %q, want passthrough %q", size, got, size) + } + } +} + func TestNormalizeGrokImageResolution(t *testing.T) { if got := normalizeGrokImageResolution("1k"); got != "1k" { t.Fatalf("1k = %q", got)