fix: 兼容像素分辨率参数

This commit is contained in:
2026-06-08 23:21:14 +08:00
parent 97f29ed156
commit a8fa8dd212
3 changed files with 124 additions and 2 deletions
@@ -29,7 +29,7 @@ func validateAndAdjustAspectRatio(aspectRatio string, capability map[string]any,
if containsString(allowed, aspectRatio) {
return aspectRatio, true
}
return allowed[0], true
return closestAspectRatio(aspectRatio, allowed), true
}
func isMediaModelTypeWithAspectRatio(capability map[string]any) bool {
@@ -445,6 +445,33 @@ func adjustAspectRatioToRange(value string, minValue float64, maxValue float64,
return ratioString(maxValue)
}
func closestAspectRatio(value string, allowed []string) string {
if len(allowed) == 0 {
return value
}
current, ok := aspectRatioNumber(value)
if !ok {
return allowed[0]
}
closest := ""
minDiff := math.Inf(1)
for _, candidate := range allowed {
ratio, ok := aspectRatioNumber(candidate)
if !ok {
continue
}
diff := math.Abs(ratio - current)
if diff < minDiff {
minDiff = diff
closest = candidate
}
}
if closest != "" {
return closest
}
return allowed[0]
}
func ratioString(value float64) string {
if value <= 0 {
return "1:1"