简体   繁体   English

使用Node.js中的GraphicsMagick / ImageMagick包在AWS Lambda中将JPG转换为WebP

[英]Converting a JPG to WebP in AWS Lambda using GraphicsMagick/ImageMagick packages in Node.js

I am leveraging the AWS Lambda example script to resize a JPG image using Node.js and ImageMagick/GraphicsMagick libraries. 我正在利用AWS Lambda 示例脚本使用Node.js和ImageMagick / GraphicsMagick库调整JPG图像的大小。 I want to make a simple modification to convert the image from a JPG to a WebP format after the resize. 我想做一个简单的修改,在调整大小后将图像从JPG转换为WebP格式。 (GraphicsMagick does not support WebP, but ImageMagick does, which is subclassed in the script) . (GraphicsMagick不支持WebP,但是ImageMagick会支持WebP中的子类) This should be possible with the following block of code, as per the example in the Buffers section here (which converts JPG to PNG). 这应该是可能与以下代码块,按照在缓冲器部分的示例这里 (其转化JPG到PNG)。

gm('img.jpg')
.resize(100, 100)
.toBuffer('PNG',function (err, buffer) {
  if (err) return handle(err);
  console.log('done!');
})

When I run that block of code in my local Node.js installation (replacing PNG with WebP), it works. 当我在本地Node.js安装中运行该代码块(用WebP替换PNG)时,它可以工作。

When I modify the transform function (see below) of the AWS Lambda example script , however, and execute it on AWS, I receive the following "Stream yields empty buffer" error: 但是,当我修改AWS Lambda 示例脚本的转换函数(见下文)并在AWS上执行它时,我收到以下“Stream yield empty buffer”错误:

Unable to resize mybucket/104A0378.jpg and upload to mybucket_resized/resized-104A0378.jpg due to an error: Error: Stream yields empty buffer

Modified transform() function (see the line with 'webp'): 修改了transform()函数(参见'webp'行):

function tranform(response, next) {
            gm(response.Body).size(function(err, size) {
                // Infer the scaling factor to avoid stretching the image unnaturally.
                var scalingFactor = Math.min(
                    MAX_WIDTH / size.width,
                    MAX_HEIGHT / size.height
                );
                var width  = scalingFactor * size.width;
                var height = scalingFactor * size.height;

                // Transform the image buffer in memory.
                this.resize(width, height)
                    .toBuffer('webp', function(err, buffer) {
                        if (err) {
                            next(err);
                        } else {
                            next(null, response.ContentType, buffer);
                        }
                    });
            });
        }

I realize that the response.ContentType is still equal to image/jpeg, but I don't think that is playing a role here. 我意识到response.ContentType仍然等于image / jpeg,但我不认为这是在这里发挥作用。 Also, I realize that I should probably convert to WebP before resizing but...baby steps! 此外,我意识到我应该在调整大小之前转换为WebP但是......宝贝步骤!

Any ideas? 有任何想法吗?

I have encountered the same error, "Stream yields empty buffer", in other operations using gm and AWS lambda. 我在使用gm和AWS lambda的其他操作中遇到了同样的错误,“Stream yield empty buffer”。

Turns out that the lambda container ran out of memory. 事实证明lambda容器内存不足。

I tested this assumption using a large image that was constantly throwing an error. 我使用一个不断抛出错误的大图像测试了这个假设。

When i increased the memory of the lambda function everything worked perfectly. 当我增加lambda函数的记忆时,一切都很完美。

Hope this helps you as well 希望这对你也有帮助

ImageMagick has to be specifically compiled with WebP support. 必须使用WebP支持专门编译ImageMagick。 My experimenting seems to show that the ImageMagick on AWS Lambda is not compiled with WEBP :( 我的实验似乎表明AWS Lambda上的ImageMagick不是用WEBP编译的:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM