Following the layer creation steps here: https://www.youtube.com/watch?v=JdJo-_Y1ZIM that specifically install the layer: https://serverlessrepo.aws.amazon.com/applications/us-east-1/145266761615/image-magick-lambda-layer
The code works -- I'm able to resize images just fine. Note:
const GM = require('gm');
const gm = GM.subClass({ imageMagick: true });
exports.resize = async (buf, width, height) => { // WORKS!!
return new Promise((resolve, reject) => {
gm(buf).resize(width, height).noProfile().toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
});
};
exports.osdStamp = async (buf, message, gravity) => { // DOES NOT WORK
return new Promise((resolve, reject) => {
gm(buf)
.fontSize(40)
.drawText(10,50, message)
.toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
});
};
The output buffer is created (and subsequently saved to S3 successfully) but it does not have any text on it.
I have also tried the suggestion in this question Draw text on image using graphicsmagick (node-gm) drawText
gm(buf).fill('#ffffff').font('Arial', 27).drawText(225, 75, "Some text");
This did not put text on the image.
I can't help but wonder if it's related to not having fonts as the question-asker guessed in this question: How to use ImageMagick `drawtext` method in AWS Lambda function?
So I have tried downloading a ttf font, including it in my package, and referencing it:
gm(buf)
.fill('#ffffff')
.font('./Hack-Regular.ttf', 27)
.drawText(10,50, message)
.toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
});
I also tried an absolute path.
gm(buf)
.fill('#ffffff')
.font('/var/task/fonts/Hack-Regular.ttf')
.drawText(10,50, "SOME TEXT!!!SOME TEXT!!!SOME TEXT!!!SOME TEXT!!!SOME TEXT!!!")
.toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
Further, I have verified the font file is there with:
var cp = require('child_process');
var ls = cp.spawnSync('ls', ['/var/task/fonts'], { encoding: 'utf8' });
console.log('stdout here: ' + ls.stdout);
And getting the output: 'Hack-Regular.ttf'
Note, I'm not getting any errors. I'm simply not seeing any text on the image.