diff --git a/astrbot/core/star/__init__.py b/astrbot/core/star/__init__.py index ec1ee655b..f871c67c8 100644 --- a/astrbot/core/star/__init__.py +++ b/astrbot/core/star/__init__.py @@ -18,10 +18,12 @@ class Star(CommandParserMixin): """将文本转换为图片""" return await html_renderer.render_t2i(text, return_url=return_url) - async def html_render(self, tmpl: str, data: dict, return_url=True) -> str: + async def html_render( + self, tmpl: str, data: dict, return_url=True, options: dict = None + ) -> str: """渲染 HTML""" return await html_renderer.render_custom_template( - tmpl, data, return_url=return_url + tmpl, data, return_url=return_url, options=options ) async def terminate(self): diff --git a/astrbot/core/utils/t2i/network_strategy.py b/astrbot/core/utils/t2i/network_strategy.py index f8f54b0dc..49588204f 100644 --- a/astrbot/core/utils/t2i/network_strategy.py +++ b/astrbot/core/utils/t2i/network_strategy.py @@ -34,18 +34,22 @@ class NetworkRenderStrategy(RenderStrategy): self.BASE_RENDER_URL += "/text2img" async def render_custom_template( - self, tmpl_str: str, tmpl_data: dict, return_url: bool = True + self, + tmpl_str: str, + tmpl_data: dict, + return_url: bool = True, + options: dict = None, ) -> str: """使用自定义文转图模板""" + default_options = {"full_page": True, "type": "jpeg", "quality": 40} + if options: + default_options.update(options) + post_data = { "tmpl": tmpl_str, "json": return_url, "tmpldata": tmpl_data, - "options": { - "full_page": True, - "type": "jpeg", - "quality": 40, - }, + "options": default_options, } if return_url: ssl_context = ssl.create_default_context(cafile=certifi.where()) diff --git a/astrbot/core/utils/t2i/renderer.py b/astrbot/core/utils/t2i/renderer.py index 73e8eee66..ddfd2d0da 100644 --- a/astrbot/core/utils/t2i/renderer.py +++ b/astrbot/core/utils/t2i/renderer.py @@ -16,19 +16,24 @@ class HtmlRenderer: self.network_strategy.set_endpoint(endpoint_url) async def render_custom_template( - self, tmpl_str: str, tmpl_data: dict, return_url: bool = False + self, + tmpl_str: str, + tmpl_data: dict, + return_url: bool = False, + options: dict = None, ): """使用自定义文转图模板。该方法会通过网络调用 t2i 终结点图文渲染API。 @param tmpl_str: HTML Jinja2 模板。 @param tmpl_data: jinja2 模板数据。 + @param options: 渲染选项。 @return: 图片 URL 或者文件路径,取决于 return_url 参数。 @example: 参见 https://astrbot.app 插件开发部分。 """ - local = locals() - local.pop("self") - return await self.network_strategy.render_custom_template(**local) + return await self.network_strategy.render_custom_template( + tmpl_str, tmpl_data, return_url, options + ) async def render_t2i( self, text: str, use_network: bool = True, return_url: bool = False