Node.js的gm包调整图片大小出错

4

我已经尝试了一段时间,但我一直得到这个错误:

Error: Command failed: Invalid Parameter - /images

我已经安装了ImageMagick和gm包,所以这绝对不是问题。
    gm(imageLocation)
      .resize(100) // use your own width and height
      .write('here.jpg', function (err) {
        if (!err) console.log(' hooray! ');
        else console.log(err); 
      });

imageLocation./images/3.jpg。为什么这个错误一直出现?我查看了文档。

我使用的是Windows 32位机器。我的服务器应该从文件夹中获取图像,调整其大小,然后显示它。似乎我必须写入调整大小的照片,然后显示它,但是写入过程总是出错,图像最终为空。

如果有一种方法可以跳过写入部分,直接显示照片,那也太棒了。

谢谢!


我使用的URL查询: http://localhost:8123/images/3.jpg

完整代码:

var querystring = require('querystring'); //used for parsing parts of urls
    url = require('url'); 
    http = require('http'); 
    fs = require('fs'); 
    gm = require('gm').subClass({ imageMagick: true });; 

var server = http.createServer(); 

server.on('request', function(request, response){
    var parsed_url = url.parse(request.url, true); //true gets the query as well

    imageLocation = '.' + parsed_url.pathname;
    gm(imageLocation)
      .resize(100) // use your own width and height
      .write('here.jpg', function (err) {
        if (!err) console.log(' hooray! ');
        else console.log(err); 
      });


    if (getImage('here.jpg', response)){
        //image is displayed 
    }
    else{
    respond404(parsed_url.pathname, response); 
    }

})

function respond404(path, response){
    respond(404, "The requested path " + path + " was not found", response)  
}

function getImage(location, response)
{
    try{
        var img = fs.readFileSync(location);
        response.writeHead(200, {'Content-Type':'image/jpg'}); //parse this end
        response.end(img, 'binary');
        return true;
    }catch(e){
        return false; 
    }    
}

server.listen(8123); 
2个回答

3

Svbaker提供的答案可以在Linux(也许Mac上)使用。

对于Windows,我通过以管理员模式打开“命令行”并在那里启动服务器来使其工作。


1
我通过修改您对 gm 的引用方式,成功运行了您的代码。
var gm = require('gm');

我还需要记住在我的情况下使用正确的权限执行node:
sudo node server.js

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接