how to use the imagemin buffer function

4.5k views Asked by At

I'm trying to use the imagemin buffer function: 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);
     });

for some reason its not working. does anyone know why?

1

There are 1 answers

0
vinfinit On

imagemin.buffer function works right, but you can have problem with end event. I believe your function has never been called.

Below you can see example with bluebird modification.

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)
  })