目前找到一个 next/og 库,但是生成的是一个图像响应。有没有类似的可以直接生成图像的库,最好支持生成多种图像格式。
import { ImageResponse } from 'next/og'
// Image metadata
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
// Image generation
export default async function Image() {
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 128,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
About Acme
</div>
),
// ImageResponse options
{
...size,
}
)
}
ImageResponse 可以通过 jsx 渲染为图像,并为客户端响应该图像。但是这个专为 SEO 服务的,有没有更通用的库,不用直接返回图像响应,返回图像就可以了。参数也不要求必须是 jsx(本质上 ReactNode),可以是 html 字符串。运行环境在 node 环境。
jsx 是模版,跟生成图像没有任何关系。
js 当然可以生成图像,比如 html2canvas。
og 是 SEO 优化用的,跟你的需求无关。