From 2bf047b8ad7806803e3ddf66c7d29a7219d4688b Mon Sep 17 00:00:00 2001 From: ddCat <2695092045@qq.com> Date: Wed, 5 Aug 2026 10:28:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(canvas):=20=E5=9B=BE=E7=94=9F=E5=9B=BE?= =?UTF-8?q?=E6=AF=94=E4=BE=8B=20-=20=E9=A2=84=E8=AE=BE=E6=AF=94=E4=BE=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=B8=8A=E6=B8=B8=E5=83=8F=E7=B4=A0?= =?UTF-8?q?=E5=B0=BA=E5=AF=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #137 --- backend/internal/service/provider.go | 21 ++++++++++++++++++++- backend/internal/service/provider_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/internal/service/provider.go b/backend/internal/service/provider.go index d3b54bcfe..3c3e56775 100644 --- a/backend/internal/service/provider.go +++ b/backend/internal/service/provider.go @@ -2267,9 +2267,28 @@ func normalizeImageQuality(value string) string { func normalizePixelSize(value string) string { value = strings.TrimSpace(value) - if value == "" || value == "auto" || strings.Contains(value, ":") { + if value == "" || value == "auto" { return "" } + // 画布按比例保存常用预设;图片接口只接受像素尺寸,必须在请求边界完成转换。 + switch value { + case "1:1": + return "1024x1024" + case "3:2": + return "1536x1024" + case "2:3": + return "1024x1536" + case "4:3": + return "1360x1024" + case "3:4": + return "1024x1360" + case "16:9": + return "1824x1024" + case "9:16": + return "1024x1824" + case "21:9": + return "2352x1008" + } if strings.Contains(value, "x") { return value } diff --git a/backend/internal/service/provider_test.go b/backend/internal/service/provider_test.go index dce1207a8..5d087a07a 100644 --- a/backend/internal/service/provider_test.go +++ b/backend/internal/service/provider_test.go @@ -104,6 +104,26 @@ func TestVolcengineArkImageRejectsMaskBeforeRequest(t *testing.T) { } } +func TestNormalizePixelSizeConvertsCanvasAspectRatios(t *testing.T) { + tests := map[string]string{ + "1:1": "1024x1024", + "3:2": "1536x1024", + "2:3": "1024x1536", + "4:3": "1360x1024", + "3:4": "1024x1360", + "16:9": "1824x1024", + "9:16": "1024x1824", + "21:9": "2352x1008", + } + for input, want := range tests { + t.Run(input, func(t *testing.T) { + if got := normalizePixelSize(input); got != want { + t.Fatalf("normalizePixelSize(%q) = %q, want %q", input, got, want) + } + }) + } +} + func TestDoBinaryRejectsOversizedProviderResponse(t *testing.T) { t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {