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
@@ -207,6 +207,14 @@ func (imageSizeProcessor) Process(params map[string]any, modelType string, conte
width, height = constrainImageDimensions(width, height, capability)
params["width"] = width
params["height"] = height
if stringFromAny(params["aspect_ratio"]) == "" {
aspectRatio := aspectRatioFromDimensions(width, height)
allowed := aspectRatioAllowed(capability["aspect_ratio_allowed"], firstNonEmptyString(stringFromAny(params["resolution"]), context.resolution))
if processed, ok := validateAndAdjustAspectRatio(aspectRatio, capability, allowed); ok && processed != "" {
params["aspect_ratio"] = processed
context.aspectRatio = processed
}
}
resolution := normalizeImageResolutionForCapability(firstNonEmptyString(stringFromAny(params["resolution"]), context.resolution), width, height, capability)
if resolution != "" {
params["resolution"] = resolution
@@ -248,7 +256,10 @@ func imageDimensionsFromParams(params map[string]any) (int, int, bool) {
if width > 0 && height > 0 {
return width, height, true
}
return parsePixelSizeString(stringFromAny(params["size"]))
if width, height, ok := parsePixelSizeString(stringFromAny(params["size"])); ok {
return width, height, true
}
return parsePixelSizeString(stringFromAny(params["resolution"]))
}
func imageSizeCapabilityConfigured(capability map[string]any) bool {
@@ -431,6 +442,30 @@ func imageResolutionFromDimensions(width int, height int) string {
}
}
func aspectRatioFromDimensions(width int, height int) string {
if width <= 0 || height <= 0 {
return ""
}
divisor := gcd(width, height)
return fmt.Sprintf("%d:%d", width/divisor, height/divisor)
}
func gcd(a int, b int) int {
if a < 0 {
a = -a
}
if b < 0 {
b = -b
}
for b != 0 {
a, b = b, a%b
}
if a == 0 {
return 1
}
return a
}
func closestImageResolution(target string, allowed []string) string {
order := []string{"1K", "2K", "3K", "4K", "8K"}
targetIndex := indexOfString(order, target)