简体   繁体   English

图像使用Node.js从Amazon S3动态调整大小

[英]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.

相关问题 Node.js-将画布另存为图像(适用于使用Amazon S3的Heroku托管应用程序) - Node.js - Saving canvas as an image [for Heroku hosted apps using Amazon S3] 通过 Node.js 将 base64 编码图像上传到 Amazon S3 - Uploading base64 encoded Image to Amazon S3 via Node.js 使用 Node.js 将图像上传到 Amazon S3 会产生一个小的透明方块 - Uploading image to Amazon S3 with Node.js results in a small transparent square 如何使用 Node.js 的 AWS SDK 将 Amazon S3 中的所有对象从一个前缀复制/移动到另一个前缀 - How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js Node.js上传到Amazon S3工作但文件损坏 - Node.js upload to Amazon S3 works but file corrupt 将远程文件数组流式传输到Node.js中的Amazon S3 - Stream array of remote files to amazon S3 in Node.js 如何从Node.js中的Amazon S3存储桶同步下载文件 - How to download a file from Amazon S3 bucket in node.js synchronously 我是否需要使用队列将文件从Node.js服务器上传到Amazon S3? - Do I need to use queue for uploading file to Amazon S3 from my Node.js Server? Node.js knox s3图像检索 - Node.js knox s3 image retrieval 如何使用node.js从cloudinary下载图像 - How to download image/s from cloudinary using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM