gpt4 book ai didi

javascript - 使用特殊的 unicode 渲染文本图像?

转载 作者:行者123 更新时间:2023-11-30 20:44:44 24 4
gpt4 key购买 nike

我想生成并提供文本图像。

目前我正在尝试使用 text2png :

text2png('this string', {font: '14px Courier', bgColor: 'white'})

但是text2png好像不能做unicode/emoji。

它正在将 🔥 变成 enter image description here和汉字,例如中文, 成 enter image description here .

有没有办法将包含这些字符的字符串渲染成图像?

text2png 在其他方面表现完美。

最佳答案

text2png 目前无法执行此操作。

它依赖于 node-canvas,而后者又依赖于 cairo。在撰写本文时 Cairo 的稳定版本 (1.14.12) 似乎不支持表情符号。

不过cairo开发版(1.15.8)支持emoji。

Features and Enhancements

  • Support colored emoji glyphs, stored as PNG images in OpenType fonts.

所以也许构建并尝试强制 node-canvas 使用这个更高版本的 cairo 会成功。


现在,我找到了这个包 wkhtmltox .它将 HTML 转换为图像(或 PDF,但我不会使用它)。

确保您拥有命令行工具 wkhtmltoimage也安装了。 node.js 包 wkhtmltox 依赖于它来生成图像。 (我们不关心 wkhtmltopdf)。

将所有文本弹出到一些外部 HTML 文档中,./content.html:

<!doctype html>
<html>
<head><meta charset="utf-8"></head>
<body>
Here's some 中文 letters!
</body>
</html>

然后渲染这个:

const fs = require('fs');
const wkhtmltox = require('wkhtmltox');

const converter = new wkhtmltox();

converter.image(fs.createReadStream('content.html'), { format: "png" })
.pipe(fs.createWriteStream('image.png'));

我们在这里通过管道传输 PNG 缓冲区,您也可以通过管道传输到 HTTP 响应对象(好吧,任何可写流)。

wkhtmltox 将像浏览器一样呈现所有内容,据我所知(甚至支持样式等),只要您安装了字体。

因此请确保您安装了带有中文字形的字体。

我自己还没有测试过表情符号,但我认为只要你有正确的字体它就会工作。

关于javascript - 使用特殊的 unicode 渲染文本图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48843181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com