added output image resize
added output image resize

--- a/onvif/snapshot.html
+++ b/onvif/snapshot.html
@@ -15,6 +15,10 @@
         <label for="node-input-password"><i class="fa fa-key"></i> Password</label>
         <input type="text" id="node-input-password" placeholder="Password">
     </div>
+    <div class="form-row">
+        <label for="node-input-resize"><i class="fa fa-file-code-o"></i> Resize</label>
+        <input type="text" id="node-input-resize" placeholder='{"width": 800}'>
+    </div>
 </script>
 
 <script type="text/x-red" data-help-name="ONVIF Snapshot">
@@ -29,6 +33,10 @@
     </thead>
     <tbody>
         <tr>
+            <td><code>name</code></td>
+            <td>IP camera name</td>
+        </tr>
+        <tr>
             <td><code>url</code></td>
             <td>IP camera URL</td>
         </tr>
@@ -39,6 +47,14 @@
         <tr>
             <td><code>password</code></td>
             <td>IP camera password</td>
+        </tr>
+        <tr>
+            <td><code>resize.width</code></td>
+            <td>Resize image by width</td>
+        </tr>
+        <tr>
+            <td><code>resize.height</code></td>
+            <td>Resize image by height</td>
         </tr>
     </tbody>
 </table>

--- a/onvif/snapshot.js
+++ b/onvif/snapshot.js
@@ -1,48 +1,32 @@
 module.exports = (RED) => {
     "use strict";
     let onvif = require("node-onvif");
+    const sharp = require('sharp');
 
     function snapshot(config) {
         RED.nodes.createNode(this, config);
         let node = this;
 
         node.on("input", function (msg) {
-            try {
-                let _msg = JSON.parse(msg.payload);
-                if (typeof _msg === "object") {
-                    if(_msg.hasOwnProperty("name")) {
-                        msg.name = _msg.name;
-                    }
-                    if(_msg.hasOwnProperty("url")) {
-                        msg.url = _msg.url;
-                    }
-                    if(_msg.hasOwnProperty("username")) {
-                        msg.username = _msg.username;
-                    }
-                    if(_msg.hasOwnProperty("password")) {
-                        msg.password = _msg.password;
-                    }
-                }
-            }
-            catch (ex) {}
 
-            config.name     = msg.name || config.name;
-            config.url      = msg.url || config.url;
-            config.username = msg.username || config.username;
-            config.password = msg.password || config.password;
+            config.name     = msg.payload.name      || config.name;
+            config.url      = msg.payload.url       || config.url;
+            config.username = msg.payload.username  || config.username;
+            config.password = msg.payload.password  || config.password;
+            config.resize   = msg.payload.resize    || config.resize;
 
             if(msg.hasOwnProperty("payload")) {
                 msg._payload = msg.payload;
             }
             msg.node = this.type;
 
+            if (!config.url) {
+                node.warn("No URL is specified. Please specify in node configuration.");
+                return;
+            }
+
             run(msg, node, config);
         });
-
-        if (!config.url) {
-            node.warn("No URL is specified. Please specify in node configuration.");
-            return;
-        }
     }
     RED.nodes.registerType("ONVIF Snapshot", snapshot);
 
@@ -58,15 +42,35 @@
             return onvifInstance.fetchSnapshot();
         }).then((res) => {
             let prefix = 'data:' + res.headers['content-type'] + ';base64,';
-            let base64Image = Buffer.from(res.body, 'binary').toString('base64');
-            msg.payload = {
-                config: config,
-                image: {
-                    base64: (prefix + base64Image),
-                    binary: res.body
-                }
-            };
-            node.send(msg);
+
+            if (config.resize) {
+                sharp(Buffer.from(res.body, 'binary'))
+                    .resize(config.resize)
+                    .toFormat('png')
+                    .toBuffer()
+                    .then( data => {
+                        msg.payload = {
+                            config: config,
+                            image: {
+                                base64: (prefix + data.toString('base64')),
+                                binary: res.body
+                            }
+                        };
+                        node.send(msg);
+                    }).catch( err => {
+
+                    });
+            } else {
+                let base64Image = Buffer.from(res.body, 'binary').toString('base64');
+                msg.payload = {
+                    config: config,
+                    image: {
+                        base64: (prefix + base64Image),
+                        binary: res.body
+                    }
+                };
+                node.send(msg);
+            }
         }).catch((error) => {
             msg.payload = null;
             msg.error = error;

--- a/package.json
+++ b/package.json
@@ -1,13 +1,14 @@
 {
     "name": "node-red-contrib-onvif",
-    "version": "0.0.3",
+    "version": "1.0.0",
     "description": "A Node-RED node that interacts with ip cameras using the ONVIF protocol",
     "repository": {
         "type": "git",
         "url": "https://github.com/razvanstanga/node-red-contrib-onvif.git"
     },
     "dependencies": {
-        "node-onvif": "^0.1.7"
+        "node-onvif": "^0.1.7",
+        "sharp": "^0.26.2"
     },
     "inputs": 1,
     "keywords": [

comments