如何根据text/string在nodejs中render/generate图像?

How to render/generate image according to text/string in nodejs?

我如何生成具有以下内容的字符串的图像:

基本上,用户会发送一个关于他希望自己的图像如何的请求。

但是当我收到请求时,我应该如何使用 nodejs 渲染 png 或 base64 url 将其发送回给用户。是否有任何库或方法可以实现此目的。

我之前做了一些研究,似乎没有一个框架可以帮助渲染具有字体和文本样式(如浮雕)的文本

更新 - 这将涵盖更新图像的属性,而不是从图像中提取文本并更新它 - 您可能需要一个图像分析库

使用锐利库根据需要处理图像。 https://github.com/lovell/sharp http://sharp.dimens.io/en/stable/

一个调整大小的简单示例(文档将展示如何在此之外进行您想要的更改):

const request = require('request').defaults({ encoding: null });

request.get(imgUrl, params, function (err, res, body) {
        sharp(body)
        .resize(params.width, params.height)
        .toFormat('jpeg')
        .toBuffer()
        .then((outputBuffer) => {
          // outputBuffer contains JPEG image data no wider than params.width and no higher
          // than params.height while maintaining quality of image.
          let output = "data:" + res.headers["content-type"] + ";base64," + new Buffer(outputBuffer).toString('base64');
          return output;
        });

这里输出的是base64图片

您可以尝试节点 canvas 实现:https://github.com/Automattic/node-canvas

如果您使用浏览器 js canvas,基本上您可以 "draw" 任何您想要的东西 canvas,但有些事情可能会有所不同