On the fly image resizing from Amazon S3 with Node.js

I have a bucket with many images and I want to resize one of them on the fly, from the buffer returned from Amazon S3's getObject () method, but I cannot find a library to resize directly from the memory buffer. Thumbbot and Imagemagick are reading a file from disk and that's not what I want!

This is some sample code:

exports.resizeImage = function(req, res) {
    bucket.getObject({Key: 'image.jpg'}, function(err, data){
        if (err) throw err;
        var image = resizeImage(data.Body);//where resize image is a method or a library that I need to call
        res.writeHead(200, {'Content-Type': data.ContentType });
        res.end(image); //send a resized image buffer
    });
};

      

I appreciate any help, thanks

+3


source to share


1 answer


I found a solution using a library gm

for node js



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

exports.resizeImage = function(req, res) {
    bucket.getObject({Key: 'image.jpg'}, function(err, data){
        if (err) throw err;
        gm(data.Body, 'ok.jpg')
            .size({bufferStream: true}, function(err, size) {
                this.resize(200, 200);
                this.toBuffer('JPG',function (err, buffer) {
                    if (err) return handle(err);
                    res.writeHead(200, {'Content-Type': data.ContentType });
                    res.end(buffer);
                });
            });
    });
};

      

+4


source







All Articles