无法使用imageminMozjpeg压缩大文件?

时间:2018-02-20 11:50:22

标签: node.js multer imagemin

我一直在节点上创建一个服务器,它可以在压缩后获取图像文件并上传到磁盘上。 但是,正在压缩尺寸较小的图像,大小为10mb左右的图像会出现以下错误。

 { Error: Error in file: ./uploads/ecqrwxgz6lw.jpg

write EOF
    at _errnoException (util.js:1024:11)
    at WriteWrap.afterWrite [as oncomplete] (net.js:867:14)
  code: 'EOF',
  errno: 'EOF',
  syscall: 'write',
  killed: false,
  stdout: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 ff fe 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0d 00 0f 01 02 00 12 00 00 00 ac 00 ... >,
  stderr: <Buffer >,
  failed: true,
  signal: null,
  cmd: 'D:\\Projects\\web-development\\Node.js\\Business\\node_modules\\mozjpeg\\vendor\\cjpeg.exe -quality 60',
  timedOut: false }

我的multer上传文件的代码是

    var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (req, file, callback) {

        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        var  rand = Math.random().toString(36).substr(2, 12);
        req.rand =  rand;
        ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
        callback(null, rand+ext);
    }
});

var upload = multer({
    storage: storage
}).single('userPhoto');

module.exports = {
    upload: upload,
};

这是我上传文件的路线

    app.post('/events/upload', upload, function (req, res) {

    var fb_id = req.user.id;
    var email = req.user._json.email;
    var random = req.rand;

    console.log(fb_id);

    var query = {
            'Fbid': fb_id
        },
        update = {
            $set: {
                email: email
            },
            $push: {
                events: {
                    event: place,
                    imageId: random
                }
            }
        },
        options = {
            upsert: true
        };

    usersuploadImformation.findOneAndUpdate(query, update, options, function (err, data) {
        if (err) {
            console.log("Not able to update");
        }
        else{
            console.log("Updated");
        }
    });


    imagemin(['./uploads/'+random+'.{JPG,jpg}'], './uploads', {
        plugins: [
            imageminMozjpeg({
                              quality: 60,
                              progressive: true

                             })
        ]
    }).then(files => {
        console.log(files);
        //=> [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …] 
    }).catch((err)=>{
        console.log(err);
    });



});

1 个答案:

答案 0 :(得分:1)

刚刚使用另一个插件进行压缩,这是imageminmozjpeg的错误。 我使用了imageminJpegRecompress