Converting a PNG buffer to a JPG in graphicsmagick in node

Iuthisaam Muhammad Saeed picture Iuthisaam Muhammad Saeed · May 20, 2013 · Viewed 8.2k times · Source

How do I convert a png buffer to a jpg without writing to a file?

Answer

smremde picture smremde · Jan 16, 2015

You can use https://github.com/aheckmann/gm

var gm = require('gm')

// convert a buffer to a stream
gm(buffer, 'img.png')
.stream('jpg')
.pipe(outputStream);


// convert a buffer to a buffer
gm(buffer, 'img.png')
.toBuffer('PNG',function (err, buffer) {
   if (err) return handle(err);
   console.log('done!');
})