baodan/api/insurance/poster/render_validation.py
wsb1224 e1770be4cc 现在选择“长图海报”后,在“海报尺寸”下方可以切换:
自动高度
自定义高度:1920–30000px,默认 4500px
自定义高度会同步影响画布预览、任务快照、renderDocument、历史恢复和最终 PNG。若内容超过指定高度,系统会阻止导出并提示最低所需高度,不会静默裁掉底部内容。
关键修改:
[PosterCreativePanel.vue (line 51)](D:/work/code/python/coding/baodanagent/frontend/src/components/poster/workspace/PosterCreativePanel.vue:51)
[PosterHtmlCanvas.vue (line 1)](D:/work/code/python/coding/baodanagent/frontend/src/components/poster/long/PosterHtmlCanvas.vue:1)
[poster-exporter.ts (line 95)](D:/work/code/python/coding/baodanagent/frontend/src/utils/poster-exporter.ts:95)
[format_registry.py (line 17)](D:/work/code/python/coding/baodanagent/api/insurance/poster/format_registry.py:17)
[render_validation.py (line 13)](D:/work/code/python/coding/baodanagent/api/insurance/poster/render_validation.py:13)
验证结果:33 项海报测试通过、Vue 类型检查通过、生产构建通过、UI 静态检查通过
2026-07-31 22:05:57 +08:00

58 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""浏览器合成海报的文件与尺寸校验。"""
import io
from PIL import Image
from insurance.poster.format_registry import list_poster_formats
class PosterRenderValidationError(ValueError):
"""最终海报文件不符合格式契约。"""
def validate_rendered_png(
image_bytes: bytes,
format_id: str,
expected_height: int | None = None,
) -> dict:
"""完整解码 PNG并验证其真实像素尺寸。"""
format_spec = next(
(item for item in list_poster_formats() if item["id"] == format_id),
None,
)
if not format_spec:
raise PosterRenderValidationError(f"不支持的海报格式: {format_id}")
try:
with Image.open(io.BytesIO(image_bytes)) as image:
image.verify()
with Image.open(io.BytesIO(image_bytes)) as image:
image.load()
width, height = image.size
image_format = image.format
except Exception as exc:
raise PosterRenderValidationError("PNG 文件无法解码或已经损坏") from exc
if image_format != "PNG":
raise PosterRenderValidationError("最终海报必须是 PNG 格式")
expected = format_spec["output"]
if width != expected["width"]:
raise PosterRenderValidationError(
f"海报宽度不匹配,应为 {expected['width']}px实际为 {width}px"
)
if expected["height"] is not None and height != expected["height"]:
raise PosterRenderValidationError(
f"海报尺寸不匹配,应为 {expected['width']}×{expected['height']}"
f"实际为 {width}×{height}"
)
if expected["height"] is None and expected_height is not None and height != expected_height:
raise PosterRenderValidationError(
f"长图自定义高度不匹配,应为 {expected['width']}×{expected_height}"
f"实际为 {width}×{height}"
)
if expected["height"] is None and not 1 <= height <= 32767:
raise PosterRenderValidationError("长图高度超出浏览器安全范围")
return {"width": width, "height": height, "format": image_format}