优化截图,但是会残留大量的文件

This commit is contained in:
liuwei
2025-05-27 17:29:53 +08:00
parent 86b257ce20
commit d326467a04
2 changed files with 15 additions and 8 deletions

View File

@@ -154,7 +154,8 @@ class DifyPlugin(MessagePluginInterface):
# 如果是普通文本则在长度大于100字时转为图片发送
if len(response) > 400:
# 转图片
respath = await convert_md_str_to_image(response, "dify_output.jpg")
output_image = f"dify_output_{int(time.time())}.png"
respath = await convert_md_str_to_image(response, output_image)
await bot.send_image_message((roomid if roomid else sender), Path(respath))
else:
# 如果是普通文本,使用发送文本方法
@@ -217,7 +218,8 @@ class DifyPlugin(MessagePluginInterface):
# 如果是普通文本则在长度大于100字时转为图片发送
if len(response) > 400:
# 转图片
respath = await convert_md_str_to_image(response, "dify_output.jpg")
output_image = f"dify_output_{int(time.time())}.png"
respath = await convert_md_str_to_image(response, output_image)
await bot.send_image_message((roomid if roomid else sender), Path(respath))
else:
# 如果是普通文本,使用发送文本方法

View File

@@ -211,12 +211,17 @@ async def html_to_image(html_file, output_image):
browser = await p.chromium.launch(executable_path=browser_path)
# 业务逻辑不变
page = await browser.new_page()
await page.goto(f'file://{os.path.abspath(html_file)}')
await page.set_viewport_size({"width": 750, "height": 800})
await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True)
await browser.close()
try:
page = await browser.new_page()
await page.goto(f'file://{os.path.abspath(html_file)}')
await page.set_viewport_size({"width": 750, "height": 800})
await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True)
except Exception as e:
print(f"截图失败: {e}")
finally:
await page.close()
await browser.close()
except Exception as e:
print(f"浏览器操作失败: {e}")