Nodejs使用imagemagick调整图像大小

时间:2015-02-24 16:27:45

标签: node.js imagemagick-convert

我想尝试使用nodejsimagemagick编写图像大小调整脚本,该脚本从网址设置src路径,然后根据网址定义dst路径从网址中检索的图片名称。但是,当我运行我的程序并且图像没有调整大小时,我收到以下错误。

有谁知道为什么以及如何解决它?

错误:

{ [Error: Command failed: convert: unable to open image `favicon.ico': No such file or directory @ error/blob.c/OpenBlob/2657.
convert: no images defined `'images/dst/favicon500.ico'' @ error/convert.c/ConvertImageCommand/3187.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `favicon.ico': No such file or directory @ error/blob.c/OpenBlob/2657.
convert: no images defined `'images/dst/favicon800.ico'' @ error/convert.c/ConvertImageCommand/3187.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `favicon.ico': No such file or directory @ error/blob.c/OpenBlob/2657.
convert: no images defined `'images/dst/favicon200.ico'' @ error/convert.c/ConvertImageCommand/3187.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `favicon.ico': No such file or directory @ error/blob.c/OpenBlob/2657.
convert: no images defined `'images/dst/favicon45.ico'' @ error/convert.c/ConvertImageCommand/3187.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `'images/dst/ferrari45845.jpg'': No such file or directory @ error/blob.c/OpenBlob/2657.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `'images/dst/ferrari458200.jpg'': No such file or directory @ error/blob.c/OpenBlob/2657.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `'images/dst/ferrari458500.jpg'': No such file or directory @ error/blob.c/OpenBlob/2657.
] timedOut: false, killed: false, code: 1, signal: null }
{ [Error: Command failed: convert: unable to open image `'images/dst/ferrari458800.jpg'': No such file or directory @ error/blob.c/OpenBlob/2657.
] timedOut: false, killed: false, code: 1, signal: null }

这是我的服务器代码:

var http = require("http"),
    url = require("url"),
    im = require("imagemagick");



// create server

var server = http.createServer(function(req, res) {

    // get the full pathname
    var _originalPathname = url.parse(req.url).pathname;

    // we only need the pathname without the first "/"
    var _pathname = _originalPathname.substring(1);

    // get the file name and file extension
    var _img = _pathname.split("/").pop();

    // get file name
    var _imgName = _img.split(".").shift();

    // get file extension
    var _fileExtension = _img.split(".").pop();

    var _800px = {
        width: 800,
        srcPath: _pathname,
        dstPath: "'images/dst/" + _imgName + 800 + "." + _fileExtension + "'"
    };

    var _500px = {
        width: 500,
        srcPath: _pathname,
        dstPath: "'images/dst/" + _imgName + 500 + "." + _fileExtension + "'"
    };

    var _200px = {
        width: 200,
        srcPath: _pathname,
        dstPath: "'images/dst/" + _imgName + 200 + "." + _fileExtension + "'"
    };

    var _45px = {
        width: 45,
        srcPath: _pathname,
        dstPath: "'images/dst/" + _imgName + 45 + "." + _fileExtension + "'"
    };

    var _sizesArray = [_800px, _500px, _200px, _45px];

    var len = _sizesArray.length;


    // apply img resize to our src image looping through our array of sizes
    for (var i = 0; i < len; i++) {

        im.resize(_sizesArray[i], function(err) {
            if(err) {
                console.log(err);
            }
        });
    };

    res.end("Image resize complete!");
});

server.listen(8080);

0 个答案:

没有答案