[英]Image Resize on the fly from Amazon S3 using Node.js
I've a bucket with many images and I want to resize one of them on the fly, from a buffer returned from Amazon S3 getObject() method, but I can't find a library for resizing directly from memory buffer. 我有一个包含许多图像的存储桶,我想从Amazon S3 getObject()方法返回的缓冲区中动态调整其中一个,但我找不到直接从内存缓冲区调整大小的库。 Thumbbot and Imagemagick read the file from disk and that's not what I need!
Thumbbot和Imagemagick从磁盘读取文件,这不是我需要的!
This is a 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 我感谢任何帮助,谢谢
I found the solution using gm
library for node js 我找到了使用
gm
库为节点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);
});
});
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.