Vladimir Vladimir - 4 months ago 98
Node.js Question

How to resize image on fly(express and S3)

I am tring to get image from url and then to resize on fly without saving

var request = require('request');
request
.get('s3url')
.on('response', function (response) {
console.log(response) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.pipe(res)


I can now return same picture with
request
lib but how can I manipulate it and then return as response?

Answer

There are several npm modules that do resizing one example is sharp. You can use it in this way (example taken from the API documentation).

var transformer = sharp()
    .resize(300)
    .on('info', function(info) {
        console.log('Image height is ' + info.height);
    });

readableStream.pipe(transformer).pipe(res);

the readableStream would be the stream of your original picture.