How to do image overlay and watermark using node.js in amazon lambda function

3.5k views Asked by At

I have images hosted on amazons3 server.my client wants to process the images using amazon lambda function. I have followed the example: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html

It is fine for resizing. But I want overlay and watermarking for the images also.But not getting a way.I have tried with imagemagick[https://www.npmjs.com/package/gm] for it seems to take path as parameter and the s3 image path is not helping me. the obvious reason I think is that they can't be directly accessed as the images paths in the directory.

Any suggestion on how to achieve this. I stuck for last 3 days but not able move forward.Thanks in advance for help!!!!

1

There are 1 answers

0
Jaguilar On

It looks like your are trying to do the overlaying in the size action. A quick check over at the gm repository on GitHub showed an issue with a possible solution. It seems as thought the composite action accepts a stream to read from. In regards to your code I would suggest a refactor to chain the actions together. I'm not sure I understand where the write stream is going in your original function though. Let me know if this helps!

var scalingFactor = Math.min(
  newSize / size.width,
  newSize / size.height
);

var width  = scalingFactor * size.width;
var height = scalingFactor * size.height;

gm(response.Body)
.autoOrient()
.resize(width, height)
.gravity('SouthEast')
.draw('image Over 0,0 0,0 ' + getAppPath() + '/path/to/wm-bas.png')
.stream('PNG') // This converts the whole thing to a png, not sure if you want this
.pipe(writeStream) // Change this