如何调整节点红色流中的图像大小

How to resize images in a node-red flow

我有一个节点红色流,我从 REST API 中获取一些图像作为 png 或 jpg 格式的二进制缓冲区。

动机:有些人不注意和post非常大的图片到博客服务。由于该服务的图片存储量有限,我想监听事件流并将每张传入图片的大小调整为 "longest side = 1024",同时保持纵横比不变。

现在我的流程中有二进制对象作为缓冲区 - 但如何调整节点红色流程中的图像大小?我确实搜索了半天,但没有找到能够做到这一点的节点。有什么想法吗?

我认为目前没有 Node-RED 节点可以为您执行此操作。您可能必须编写自己的 Node-RED 节点,在 Node-RED 文档站点 here.

上有关于如何开始的说明

有一堆 ImageMagick nodes on npm which would probably be a good starting point or also the sharp 模块看起来像另一个不错的候选者。

我最终通过将条目 "jimp":"0.2.x" 添加到 package.json 依赖项并添加到 [=21= 中的 functionGlobalContext 来使 jimp 公开可用]:

functionGlobalContext: {
       mcrypto:require('crypto'),
       Jimp:require('jimp')
},

现在我可以很容易地在函数节点中使用它,只需编写:

var JIMP = global.get("Jimp");
msg.image2 = {};
JIMP.read(msg.payload).then(function(image) {
    msg.image.width = image.bitmap.width;
    msg.image.height = image.bitmap.height;

    if (image.bitmap.height > image.bitmap.width){
        if (image.bitmap.height > 800){
            image.resize(JIMP.AUTO, 800)
            msg.image2.width = image.bitmap.width;
            msg.image2.height = image.bitmap.height;
            image.getBuffer(image.getMIME(), onBuffer);
        }
    }
    else {
            if (image.bitmap.width > 800){
            image.resize(800, JIMP.AUTO)
            msg.image2.width = image.bitmap.width;
            msg.image2.height = image.bitmap.height;
            image.getBuffer(image.getMIME(), onBuffer);
        }
    }
}).catch(function (err) {
    // handle an exception
    if (err) throw err;
});

function onBuffer (err, buffer) {
    if (err) throw err;
    msg.payload = buffer;
    node.send(msg);
}

return ;

这样我就解决了我的需求。欢迎更好的想法。