【问题标题】:Resize Image From S3 with Javascript使用 Javascript 从 S3 调整图像大小
【发布时间】:2014-06-29 05:21:27
【问题描述】:

我正在使用 knox 包连接我的 S3 帐户并拉取图像,如下所示:

var picturestring;
knoxclient.get(key).on('response', function(res){
    console.log(res.statusCode);
    console.log(res.headers);
    res.setEncoding('base64');
    res.on('data', function(chunk){
        picturestring += chunk;
    });
    res.on('end', function () {
        console.log(picturestring);
        resizeimage(picturestring, done); // use the resize library in this function
    });
}).end();

之后,我想使用一个库来接收该字符串(图片字符串),调整图像大小,并返回一个新的 base64 字符串来表示调整后的图像。此时,我打算将调整大小的图像上传到 S3。

我在 Golang 中编写了一个类似的脚本,让我可以像这样调整图像大小,但是我查看过的每个 JS 大小调整库都只提供了从本地文件系统调整图像大小的示例。

有什么办法可以避免将图像从 S3 读取到文件系统中,而专注于专门处理返回的字符串??

***************更新****************************

function pullFromS3 (key, done) {
    console.log("This is the key being pulled from Amazon: ", key);
    var originalstream = new MemoryStream(null, {readable: false});
    var picturefile;
    client.get(key).on('response', function(res){
        console.log("This is the res status code: ", res.statusCode);
        res.setEncoding('base64');
        res.pipe(originalstream);
        res.on('end', function () {
            resizeImage(originalstream, key, done);
        });
    }).end();
};

function resizeImage (originalstream, key, done) {
    console.log("This is the original stream: ", originalstream.toString());
    var resizedstream = new MemoryStream(null, {readable: false});
    var resize = im().resize('160x160').quality(90);
    // getting stuck here ******
    originalstream.pipe(resize).pipe(resizedstream);
    done();
};

我似乎无法掌握从 originalstream --> 到 resize ImageMagick 函数 ---> 到 resizestream 的管道是如何工作的。理想情况下,resizestream 应该包含调整大小图像的 base64 字符串,然后我可以将其上传到 S3。

1) 如何等待管道完成,然后使用 resizedstream 中的数据?

2) 我的管道是否正确?我无法调试它,因为我不确定如何等待管道完成!

【问题讨论】:

    标签: node.js npm image-resizing


    【解决方案1】:

    我没有使用 S3,而是使用中国的本地云提供商来存储图像及其缩略图。在我的例子中,我使用 imagemagick 库和 imagemagick-streammemorystream 模块。

    imagemagick-stream 提供了一种通过 Stream 使用imagemagick 处理图像的方法,因此我不需要将图像保存在本地磁盘中。

    memorystream 提供了一种将源图像和缩略图图像二进制文件存储在内存中的方法,并且能够读取/写入 Stream。

    所以我的逻辑是

    1,从客户端 POST 请求中检索图像二进制文件。

    2、使用memorystream将图片保存到内存中

    3,上传到,在你的情况下,S3

    4、在imagemagick-stream中定义图片处理动作,例如resize为180x180

    5,在步骤 1 中使用 memorystream 从原始图像二进制文件创建一个读取流,通过管道进入在步骤 4 中创建的 imagemagick-stream,然后通过管道进入由 memorystream 创建的用于存储缩略图的新内存可写。

    6、将我在第5步得到的缩略图上传到S3。

    我的解决方案中唯一的问题是,如果出现许多巨大的图像,您的虚拟机可能会耗尽内存。但我知道这不应该发生在我的情况下,所以没关系。但你最好自己评估。

    希望这会有所帮助。

    【讨论】:

    • 你能看看我的更新吗?卡在你食谱的后面部分。
    • 我认为你可以resize.on('end', function () { done(); });,这样它会在完成后调用done,这就是我现在正在使用的。您也可以使用resizedstream.toBuffer()resizedstream 获取Buffer,然后您可以将内容转换为base64 并上传。
    猜你喜欢
    • 2021-04-25
    • 2021-06-25
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2010-09-15
    • 2012-08-19
    相关资源
    最近更新 更多