node.js - 如何使用 Dropbox Lepton Node JS 包压缩图像

标签 node.js node-modules

我正在尝试使用 Dropbox lepton NodeJS Package来压缩图像。但是,当我在 NodeJS Express 应用程序中运行以下代码时,出现以下错误。

有人可以建议我如何解决此错误吗?

这是我尝试运行的代码。

  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');
  });

以下是我收到的错误:

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)

最佳答案

我遇到了同样的问题,通过手动安装 lepton(请参阅 instructions here )并将其安装目录添加到 $PATH (在 Linux/Mac 或 Windows 中的等效项)中解决了该问题。

此代码还有另一个问题:您假设可以提供 URL 作为文件 - 但 compress 接受本地存储​​在您计算机上的文件的路径。

您可以通过在本地下载此文件来检查 lepton 是否正确安装:

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

并从命令行运行:

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

基本上,that's what node-lepton is doing :)

您应该看到 jpg 的压缩版本保存到 51di81xl3g.lep 中。

完成此操作后,您可以返回代码,添加逻辑以在压缩文件之前将文件下载到本地,这样就可以了。

示例:

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!");
                }); 
            });
        });
});

关于node.js - 如何使用 Dropbox Lepton Node JS 包压缩图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521854/

相关文章:

javascript - Node js从函数返回值

javascript - 如何将 Node js 包添加到 Meteor 应用程序中?

javascript - Webpack 2 开发工具不工作

validation - 对铁路模型进行验证

node.js - 为什么当我安装新的 npm 包时,我必须运行 "docker-compose down",然后运行 ​​ "docker-compose up --build"?

node.js - 不同版本的 Node.js 使用不同的 node_modules 目录

javascript - 无服务器功能中的数据库连接缓存代码仅执行一次

javascript - Javascript中的C解析器

javascript - 使用 nodejs google calendar API 插入事件返回 400 : "Missing end time"

javascript - 使用 node.js api 递归地监视目录