fix(canvas): 图生图比例 - 预设比例转换为上游像素尺寸

Fixes #137
This commit is contained in:
ddCat
2026-08-05 10:28:53 +08:00
parent 1fe3ce367f
commit 2bf047b8ad
2 changed files with 40 additions and 1 deletions

View File

@@ -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
}

View File

@@ -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) {