【问题标题】:Unable to compress large file with imageminMozjpeg?无法使用 imageminMozjpeg 压缩大文件?
【发布时间】:2018-07-30 18:44:36
【问题描述】:

我一直在节点上制作一个服务器,它可以获取图像文件并在压缩后上传到磁盘。 但是,较小尺寸的图像正在被压缩,而尺寸较大的图像(例如 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);
    });



});

【问题讨论】:

  • 您好,您应该在上传前使用npmjs.com/package/dev-express-uploader 包进行文件上传和压缩,只需几个步骤即可。
  • 谢谢,但这将是一个非常新的库,这意味着更少的支持。
  • 是的,我同意,但您可以轻松使用,无需任何支持,实际上我使用了它。
  • 是的...当然,我会尝试一下,如果这个没有任何问题,它看起来确实很容易。

标签: node.js multer imagemin


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多