fix: 兼容像素分辨率参数
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user