forked from soup-bowl/node-red-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharp-resize.js
40 lines (36 loc) · 1.05 KB
/
sharp-resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module.exports = function (RED) {
"use strict";
const sharp = require("sharp");
function SharpResizeNode(config) {
RED.nodes.createNode(this, config);
this.width = config.width;
this.height = config.height;
this.fit = config.fit;
this.position = config.position;
var node = this;
node.on('input', function (msg) {
const width = msg.sharp?.width ?? config.width;
const height = msg.sharp?.height ?? config.height;
const fit = msg.sharp?.fit ?? config.fit ?? 'centre';
const position = msg.sharp?.position ?? config.position ?? 'centre';
if (Buffer.isBuffer(msg.payload)) {
sharp(msg.payload)
.resize(parseInt(width), parseInt(height), {
fit: fit,
position: position
})
.toBuffer()
.then(imageBuffer => {
msg.payload = imageBuffer;
node.send(msg);
})
.catch(err => {
node.error('[sb-sharp] Error resizing image:', err);
});
} else {
node.error('[sb-sharp] The payload was not an image buffer.');
}
});
}
RED.nodes.registerType("sharp-resize", SharpResizeNode);
}