Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - convert webp to jpeg as a buffer (in memory)

Do you know any way I can convert an image/webp buffer to image/jpeg buffer without the need to use the filesystem?

First I get the buffer with request-promise and then I would like to convert it and send it away in another HTTP call.

I found this package that works on files: https://github.com/scionoftech/webp-converter

Then tried to find something useful in https://github.com/imagemin/imagemin but was not successful.

like image 293
Lukáš Křivka Avatar asked Nov 06 '22 21:11

Lukáš Křivka


2 Answers

You can use the convert functionality in ImageMagick to do so, using it through the GraphicsMagick NPM package. It accepts webp-compressed TIFFs which you can output as a jpg. When you install GraphicsMagick, make sure to also include ImageMagick and install it with a --with-webp flag so that webp is supported. When requiring the gm lib, make sure you also specify imageMagick:

 const gm = require('gm').subClass({imageMagick: true});

In the body of your promise you can call a function like this:

gm().command('convert').in('yourImage.jpg').in('yourImage.webp').toBuffer(function(err, buffer){
if(err)throw err
//...Do what you need with the buffer
});
like image 71
Asinus Rex Avatar answered Nov 14 '22 22:11

Asinus Rex


I found a very fast library for this: sharp

The syntax is really clear and easy too. To convert from a webp buffer to jpg buffer, you just do

sharp(webpBuffer).jpeg().toBuffer();
like image 36
Chloe Liliace Avatar answered Nov 14 '22 23:11

Chloe Liliace