kurrodu
kurrodu

Reputation: 2188

How to Compress image using Dropbox Lepton Node JS Package

I am trying to use Dropbox lepton NodeJS Package to compress images. However, when I run the below code in my NodeJS Express App, below is the error that I get.

Could someone please suggest how can I resolve this error ?

Here is the code that I am trying to run.

  var myLepton                 =   require('node-lepton');

  myLepton.compress('http://www.barth.com/hires/N10_JB07006.jpg',
  {
    unjailed: false,
  },
  function(err, data){
    if(err) throw err;
    console.log("data : "+JSON.stringify(data, null, 4));
    console.log('Successfully compressed image');
  });

Below is the error that I get:

Error: Command failed: lepton -memory=1024M -threadmemory=128M http://www.barth.com/hires/N10_JB07006.jpg 51di81xl3g.lep
/bin/sh: lepton: command not found

    at ChildProcess.exithandler (child_process.js:275:12)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:554:12)

Upvotes: 1

Views: 158

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53525

I had the same issue, solved it by installing lepton manually (see instructions here) and adding the directory where it's installed to $PATH (in Linux/Mac or its equivalent in Windows).

There is another problem with this code: you're assuming that you can provide a URL as your file - but compress accepts a path to a file stored locally on your machine.

You can check that lepton is installed correctly by downloading this file locally:

curl http://www.barth.com/hires/N10_JB07006.jpg > file.jpg

and running from command-line:

lepton -memory=1024M -threadmemory=128M file.jpg 51di81xl3g.lep

Basically, that's what node-lepton is doing :)

you should see a compressed version of the jpg saved into 51di81xl3g.lep.

Once you have this working you can go back to your code, add the logic to download the file locally before you compress it and you should be good.

Example:

var lepton = require('node-lepton');
var http = require('http');
var fs = require('fs');

var file = fs.createWriteStream("file.jpg");
console.log("downloading ...");
var request = http.get("http://www.barth.com/hires/N10_JB07006.jpg", function(response) {
    response.on('data', function(chunk){
            file.write(chunk);
        })
        .on('end', function(){
            file.end();
            console.log("file was downloaded successfully!");

            // compress
            console.log("compressing...");
            lepton.compress('file.jpg',
            {
                unjailed: false,
            },
            function(err, data){
                if(err) throw err;
                // save the compressed data into a new file
                fs.writeFile('compressed.z', data, function(err) {
                    if(err) {
                        return console.log(err);
                    }
                    console.log("The file was saved!");
                }); 
            });
        });
});

Upvotes: 1

Related Questions