如何在飞行中调整图像大小(快递和S3)

Vla*_*mir 2 amazon-s3 image-resizing node.js express

我想从url获取图像,然后在不保存的情况下调整大小

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)
Run Code Online (Sandbox Code Playgroud)

我现在可以用requestlib 返回相同的图片,但是我如何操作它然后作为响应返回?

vin*_*ent 6

有几个npm模块可以调整一个例子sharp.您可以通过这种方式使用它(例如,从API文档中获取).

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

readableStream.pipe(transformer).pipe(res);
Run Code Online (Sandbox Code Playgroud)

readableStream将是你原始图片的流.