aws-lambda-image icon indicating copy to clipboard operation
aws-lambda-image copied to clipboard

Can't seem to compress/resize PNG files

Open farfenugen opened this issue 9 years ago • 8 comments

Can't get this to work at all, anyone else?

farfenugen avatar Dec 04 '15 00:12 farfenugen

In test case, sample PNG image seems to reduced (assert check file size at before/after). What is the file size after resizing?

ysugimoto avatar Jan 01 '16 18:01 ysugimoto

It does not reduce the image, it just exits with "errorMessage": "Woops, image process failed: ImageMagick errError: Command failed: " when using with .png

Edit: I discovered that it works with smaller resolutions, for example resizing only to 300 and 600 works, while 900 does not. It is neither a timeout nor a memory issue according to the aws lambda log output (set it to 3 minutes and 1gb ram). Any help on this is appreciated. JPGs seem to work fine.

trinistry avatar Jan 02 '16 15:01 trinistry

I think that it is the following. It takes a little time to reduce PNGs image (in testing, it takes 3000ms of one image). Once you possibly it might be avoided in lambda's memory and timeout settings.

ysugimoto avatar Jan 03 '16 16:01 ysugimoto

I am afraid it is not. Here is the cloudwatch report with some additional logging:

START RequestId: 53673dce-b254-11e5-b29f-f9f4bc56eca7 Version: $LATEST 
2016-01-03T19:58:16.542Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    will resize to: 300 
2016-01-03T19:58:16.636Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    will resize to: 600 
2016-01-03T19:58:16.637Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    will resize to: 1200 
2016-01-03T19:58:16.639Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    reduce image type: png 
2016-01-03T19:58:18.622Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    resize done. will reduce 
2016-01-03T19:58:18.622Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    reduce image type: png 
2016-01-03T19:58:19.180Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    resize done. will reduce 
2016-01-03T19:58:19.180Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    reduce image type: png 
2016-01-03T19:58:19.819Z    53673dce-b254-11e5-b29f-f9f4bc56eca7    processing image failed. 

2016-01-03T19:58:19.820Z    53673dce-b254-11e5-b29f-f9f4bc56eca7
{
    "errorMessage": "Woops, image process failed: ImageMagick errError: Command failed: "
}
END RequestId: 53673dce-b254-11e5-b29f-f9f4bc56eca7 
REPORT RequestId: 53673dce-b254-11e5-b29f-f9f4bc56eca7  Duration: 3513.28 ms    Billed Duration: 3600 ms Memory Size: 512 MB    Max Memory Used: 68 MB  

It reports no timeout, and unfortunately does not tell what exactly went wrong.

trinistry avatar Jan 04 '16 00:01 trinistry

That error message seems to be due to this code:

https://github.com/ysugimoto/aws-lambda-image/blob/master/libs/ImageResizer.js#L35

This error causes imagemagick npm pakcage, or Lambda's ImageMagick binary.

ysugimoto avatar Jan 04 '16 02:01 ysugimoto

I found it, it is a problem of the imagemagick npm package. It will kill the child process when the buffer size is above a certain value stored in options.maxBuffer inside imagemagic.js:exec2. I can not find an easy way to set this from outside (maybe you can?), so I hack around in the imagemagick.js and set the maxBuffer value to something bigger:

function exec2(file, args /*, options, callback */) {
  var options = { encoding: 'utf8'
                , timeout: 0
                , maxBuffer: 1500*1024
                , killSignal: 'SIGKILL'
                , output: null
                };

Since this is not exactly wonderful, maybe you could think of a more elegant way to solve this?

trinistry avatar Jan 04 '16 14:01 trinistry

@trinistry did your update to the imagemagick buffer still stand? I was able to pass that part, but then stopped working again on a second execution.

Additionally, this is what I got afterwards:

Error: Command failed: convert: no decode delegate for this image format

Edit: nevermind, I seemed to have resolved it by increasing the memory to 1024 for a 5mb image. Any thoughts on how we should deal with 5mb+ images? It's going to be a nightmare.

handonam avatar Feb 06 '16 06:02 handonam

From what I can tell, I've run into the same issue - seems to happen when I try to resize PNGs to a larger size, pretty much >= 2x their size, for what it's worth.

CharlTruter avatar May 12 '16 11:05 CharlTruter