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.
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
});
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With