【问题标题】:how to use the imagemin buffer function如何使用 imagemin 缓冲功能
【发布时间】:2017-05-20 16:22:03
【问题描述】:

我正在尝试使用 imagemin 缓冲区功能: https://github.com/imagemin/imagemin

 file.on('data', function (d) {
   fileBuffer = Buffer.concat([fileBuffer, d]);
 }).on('end', function () {
   imagemin.buffer(fileBuffer, {
       plugins: [
          imageminMozjpeg(),
          imageminPngquant({quality: '80'})
       ]
     }).then(function (file) {
       console.log(file);
     }, function (err) {
       console.dir(err);
     });

由于某种原因它不起作用。有人知道为什么吗?

【问题讨论】:

    标签: node.js imagemin


    【解决方案1】:

    imagemin.buffer 函数可以正常工作,但您可能会遇到 end 事件的问题。我相信你的函数从未被调用过。

    您可以在下面看到带有 bluebird 修改的示例。

    const imagemin = require('imagemin');
    const Promise = require('bluebird');
    
    Promise.promisify(fs.readFile)('./screenshot.png')
      .then(buffer => {
        return imagemin.buffer(buffer, {
          plugins: [
              imageminMozjpeg(),
              imageminPngquant({quality: '80'})
          ]
        })
      })
      .then(outBuffer => {
        console.log(outBuffer.length);
      }).catch(err => {
        console.log(err)
      })
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多