This commit is contained in:
pandy
2026-04-26 23:11:17 +08:00
parent 3bc3eb2ec4
commit b41193e667
3 changed files with 86 additions and 7 deletions
+17 -2
View File
@@ -61,6 +61,10 @@ class PDOpenAIGPTImage2APIKey:
"default": "1024x1024",
"tooltip": "Preset output size"
}),
"use_custom_size": ("BOOLEAN", {
"default": False,
"tooltip": "Enable custom_width/custom_height size override."
}),
"custom_width": ("INT", {
"default": 0,
"min": 0,
@@ -113,6 +117,7 @@ class PDOpenAIGPTImage2APIKey:
quality,
background,
size,
use_custom_size,
custom_width,
custom_height,
num_images,
@@ -127,7 +132,12 @@ class PDOpenAIGPTImage2APIKey:
return (empty_image(), error_msg, "")
try:
resolved_size = validate_gpt_image_2_size(size, custom_width, custom_height)
resolved_size = validate_gpt_image_2_size(
size,
custom_width,
custom_height,
use_custom_size=use_custom_size,
)
except Exception as e:
return (empty_image(), f"Error: {str(e)}", "")
@@ -191,7 +201,12 @@ class PDOpenAIGPTImage2APIKey:
duration = time.time() - start_time
min_cost, max_cost = estimate_price_range_2(quality, num_images)
token_cost = calculate_token_price_2(num_images)
size_note = resolve_gpt_image_2_size(size, custom_width, custom_height)
size_note = resolve_gpt_image_2_size(
size,
custom_width,
custom_height,
use_custom_size=use_custom_size,
)
exact_cost = calculate_exact_cost_gpt_image_2_from_usage(result)
mode_label = "编辑" if is_edit_mode else "生成"
+17 -2
View File
@@ -55,6 +55,10 @@ class PDOpenAIGPTImage2AuthToken:
"default": "1024x1024",
"tooltip": "Preset output size"
}),
"use_custom_size": ("BOOLEAN", {
"default": False,
"tooltip": "Enable custom_width/custom_height size override."
}),
"custom_width": ("INT", {
"default": 0,
"min": 0,
@@ -105,6 +109,7 @@ class PDOpenAIGPTImage2AuthToken:
quality,
background,
size,
use_custom_size,
custom_width,
custom_height,
num_images,
@@ -120,7 +125,12 @@ class PDOpenAIGPTImage2AuthToken:
return (empty_image(), error_msg, "")
try:
resolved_size = validate_gpt_image_2_size(size, custom_width, custom_height)
resolved_size = validate_gpt_image_2_size(
size,
custom_width,
custom_height,
use_custom_size=use_custom_size,
)
except Exception as e:
return (empty_image(), f"Error: {str(e)}", "")
@@ -187,7 +197,12 @@ class PDOpenAIGPTImage2AuthToken:
duration = time.time() - start_time
min_cost, max_cost = estimate_price_range_2(quality, num_images)
token_cost = calculate_token_price_2(num_images)
size_note = resolve_gpt_image_2_size(size, custom_width, custom_height)
size_note = resolve_gpt_image_2_size(
size,
custom_width,
custom_height,
use_custom_size=use_custom_size,
)
exact_cost = calculate_exact_cost_gpt_image_2_from_usage(result)
mode_label = "编辑" if is_edit_mode else "生成"
+52 -3
View File
@@ -275,7 +275,40 @@ def calculate_token_price_2(n):
return cost * n
def resolve_gpt_image_2_size(size, custom_width, custom_height):
def _custom_size_enabled(use_custom_size):
if isinstance(use_custom_size, str):
return use_custom_size.strip().lower() in ("true", "1", "yes", "on")
return bool(use_custom_size)
def resolve_gpt_image_2_size(
size,
custom_width,
custom_height,
size_input=None,
width_input=None,
height_input=None,
use_custom_size=True,
):
if not _custom_size_enabled(use_custom_size):
return size
if size_input is not None:
size_input = str(size_input).strip()
if size_input:
return size_input
if width_input is not None and height_input is not None:
try:
width_input = int(width_input)
height_input = int(height_input)
except (TypeError, ValueError):
width_input = 0
height_input = 0
if width_input > 0 and height_input > 0:
return f"{width_input}x{height_input}"
if custom_width > 0 and custom_height > 0:
return f"{custom_width}x{custom_height}"
return size
@@ -326,8 +359,24 @@ def calculate_exact_cost_gpt_image_2_from_usage(result):
}
def validate_gpt_image_2_size(size, custom_width=0, custom_height=0):
resolved_size = resolve_gpt_image_2_size(size, custom_width, custom_height)
def validate_gpt_image_2_size(
size,
custom_width=0,
custom_height=0,
size_input=None,
width_input=None,
height_input=None,
use_custom_size=True,
):
resolved_size = resolve_gpt_image_2_size(
size,
custom_width,
custom_height,
size_input=size_input,
width_input=width_input,
height_input=height_input,
use_custom_size=use_custom_size,
)
if resolved_size == "auto":
return resolved_size