ysugimoto / aws-lambda-image

Automatic image resize/reduce on AWS Lambda
MIT License
824 stars 221 forks source link

Can't seem to compress/resize PNG files #8

Open farfenugen opened 8 years ago

farfenugen commented 8 years ago

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

ysugimoto commented 8 years ago

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

trinistry commented 8 years ago

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.

ysugimoto commented 8 years ago

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.

trinistry commented 8 years ago

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.

ysugimoto commented 8 years ago

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.

trinistry commented 8 years ago

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?

handonam commented 8 years ago

@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.

CharlTruter commented 8 years ago

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.