如何即时调整图片大小(express和S3) [英] How to resize image on fly(express and S3)
本文介绍了如何即时调整图片大小(express和S3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从url获取图片,然后不保存就立即调整大小
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)
我现在可以使用 request
lib返回相同的图片,但是我该如何操作它然后作为响应返回呢?
I can now return same picture with request
lib but how can I manipulate it and then return as response?
推荐答案
有几个可以调整大小的npm模块,例如 sharp
.您可以通过这种方式使用它(示例取自API文档).
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);
visibleStream
将是原始图片的流.
这篇关于如何即时调整图片大小(express和S3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文