NodeJS图像上传成功但Resize不使用GraphicsMagick

时间:2016-04-04 14:15:11

标签: node.js express fs graphicsmagick formidable

我正在尝试调整已上传的图片的大小。我已经测试了几乎可以在互联网上找到的方法,但它们似乎都没有用。我能够将图像上传到服务器的文件夹,但它仍然保持完整的图像大小。我错过了什么?

var formidable = require('formidable'),
    path = require('path'),
    fs = require('fs'),
    gm = require('gm');

//handles image upload
exports.image = function (req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        var file = files.file;
        var tempPath = file.path;

        var rename = makeid();
        var targetPath = path.resolve('./public/assets/images/upload/'+ file.name );

        fs.rename(tempPath, targetPath, function (err) {
            if (err) {
                throw err
            }
            gm( targetPath).resize(null, 50).on('finish', (function done(file) {
                console.log('done: ' + file);
            }).bind(null, file));

            return res.json({path: 'assets/images/upload/'+ file.name })
        })
    });
};

function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 15; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

0 个答案:

没有答案