Skip to content

PRINTFILM: AI short-video marketing and AI short-drama creation platform

fix: TokenFree 生图补上画幅约束,避免选 16:9 却出竖图

yi18545cd974c1
diff --git a/backend/app/services/ark.py b/backend/app/services/ark.py
index 731fed9..de6e044 100644
--- a/backend/app/services/ark.py
+++ b/backend/app/services/ark.py
@@ -878,15 +878,26 @@ class ArkGateway:
# TokenFree 按逻辑路由解析出的上游模型原样发送,不硬改模型名
chosen = (upstream_model or "").strip()
if on_tokenfree and "gpt-image" in chosen.lower():
- # Kie / gpt-image 实际按 1K·2K 档;3K/4K 钳到 2K,与计费一致
- if str(resolved_size or "").strip().upper() in {"3K", "4K"}:
+ # Kie / gpt-image 实际按 1K·2K 档;画幅靠 aspect_ratio 自然语言约束。
+ # 无 aspect_ratio 时保留 WxH,避免把唯一画幅信号压成「清晰度2K」。
+ raw_size = str(resolved_size or "").strip()
+ upper = raw_size.upper()
+ if upper in {"3K", "4K"}:
resolved_size = "2K"
+ elif "x" in raw_size.lower() and str(aspect_ratio or "").strip():
+ try:
+ w_s, _, h_s = raw_size.lower().partition("x")
+ area = int(w_s) * int(h_s)
+ resolved_size = "2K" if area >= 1500 * 1500 else "1K"
+ except ValueError:
+ resolved_size = "2K"
if on_tokenfree:
path = "/responses"
body = build_tokenfree_image_body(
model=chosen,
prompt=full_prompt,
size=str(resolved_size or ""),
+ aspect_ratio=str(aspect_ratio or ""),
ref_urls=subject_refs,
style_ref_urls=style_refs,
)
diff --git a/backend/app/services/tokenfree_image.py b/backend/app/services/tokenfree_image.py
index c559da8..26042c8 100644
--- a/backend/app/services/tokenfree_image.py
+++ b/backend/app/services/tokenfree_image.py
@@ -39,19 +39,63 @@ def tokenfree_working_image_model(model: str) -> str:
return (model or "").strip()
+def _aspect_ratio_orientation(ratio: str) -> str:
+ """根据 w:h 返回横/竖/方构图标签;无法解析则空串。"""
+ try:
+ left, _, right = (ratio or "").strip().partition(":")
+ w, h = float(left), float(right)
+ except ValueError:
+ return ""
+ if w <= 0 or h <= 0:
+ return ""
+ if w > h:
+ return "横构图"
+ if h > w:
+ return "竖构图"
+ return "方形构图"
+
+
+def _is_pixel_size(size: str) -> bool:
+ """是否为 WxH 像素串(非 1K/2K 档位)。"""
+ raw = (size or "").strip().lower()
+ if not raw or raw in {"1k", "2k", "3k", "4k"}:
+ return False
+ if "x" not in raw:
+ return False
+ left, _, right = raw.partition("x")
+ return left.isdigit() and right.isdigit()
+
+
def build_tokenfree_image_body(
*,
model: str,
prompt: str,
size: str = "",
+ aspect_ratio: str = "",
ref_urls: list[str] | None = None,
style_ref_urls: list[str] | None = None,
) -> dict[str, Any]:
- """线上成功体只要 model + 自然语言 input,不要 size: 这种协议字段。"""
+ """线上成功体只要 model + 自然语言 input,不要 size: 这种协议字段。
+
+ 画幅必须单独写入自然语言(GPT Image /responses 无结构化 size 字段),
+ 否则角色设定板等竖向提示词会盖过 UI 所选比例。
+ """
text = (prompt or "").strip() or "画面清晰,主体稳定"
+ ratio = (aspect_ratio or "").strip()
+ if ratio and ratio.lower() != "auto":
+ orient = _aspect_ratio_orientation(ratio)
+ frame = f"输出画幅必须为{ratio}"
+ if orient:
+ frame += f"({orient},整张画布严格按该比例,禁止改成其他比例)"
+ else:
+ frame += "(整张画布严格按该比例,禁止改成其他比例)"
+ text = f"{text}。{frame}"
size_s = (size or "").strip()
if size_s:
- text = f"{text}。输出清晰度{size_s}"
+ if _is_pixel_size(size_s):
+ text = f"{text}。目标像素约{size_s}"
+ else:
+ text = f"{text}。输出清晰度{size_s}"
refs = [u.strip() for u in (ref_urls or []) if (u or "").strip()]
style_refs = [u.strip() for u in (style_ref_urls or []) if (u or "").strip()]
if style_refs:
diff --git a/backend/tests/test_tokenfree_image.py b/backend/tests/test_tokenfree_image.py
index 57ab4ff..4ea34d8 100644
--- a/backend/tests/test_tokenfree_image.py
+++ b/backend/tests/test_tokenfree_image.py
@@ -33,11 +33,40 @@ def test_build_tokenfree_image_body_matches_live_success():
body = build_tokenfree_image_body(model="gpt-image-2", prompt="橘猫", size="2K")
assert body["model"] == "gpt-image-2"
assert "橘猫" in body["input"]
+ assert "输出清晰度2K" in body["input"]
assert "size:" not in body["input"]
assert "prompt" not in body
assert "watermark" not in body
+def test_build_tokenfree_image_body_includes_aspect_ratio():
+ """UI 所选画幅必须进自然语言 input,不能只写清晰度。"""
+ body = build_tokenfree_image_body(
+ model="gpt-image-2",
+ prompt="国风角色设定板",
+ size="2K",
+ aspect_ratio="16:9",
+ )
+ text = body["input"]
+ assert "输出画幅必须为16:9" in text
+ assert "横构图" in text
+ assert "输出清晰度2K" in text
+ assert "输出清晰度2816x1584" not in text
+
+
+def test_build_tokenfree_image_body_pixel_size_not_called_clarity():
+ body = build_tokenfree_image_body(
+ model="gpt-image-2",
+ prompt="场景",
+ size="2816x1584",
+ aspect_ratio="16:9",
+ )
+ text = body["input"]
+ assert "目标像素约2816x1584" in text
+ assert "输出画幅必须为16:9" in text
+ assert "输出清晰度2816x1584" not in text
+
+
def test_build_tokenfree_image_body_separates_style_and_subject_refs():
body = build_tokenfree_image_body(
model="gpt-image-2",