0

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
lowcrawler
  • 6,777
  • 9
  • 37
  • 79
  • 1
    Try using `.font('/full/path/to/SomeInstalledFont.ttf')` before `.drawText()` – Mark Setchell Feb 18 '22 at 21:25
  • 1
    Try using an **absolute** path, i.e. one beginning with `/` rather than a relative path, i.e. one beginning with `.` – Mark Setchell Feb 18 '22 at 21:33
  • @Mark Setchell No dice. Updated original question. – lowcrawler Feb 18 '22 at 21:41
  • 1
    Mmm... sorry, I don't use node (?) as I can't get on with all the braces everywhere, so I can't really read your code 100%. Are you sure that `drawText()` actually has an image or a piece of canvas to draw on? Does the thing you save have the dimensions you expect? Or can you create a solid black canvas and just draw on that? – Mark Setchell Feb 18 '22 at 21:56
  • I added a resize after the drawText and it applies the resize as expected an the saved image is correct (except the lack of text). I will attempt to draw on a blank canvas next. Thanks. – lowcrawler Feb 18 '22 at 22:13

0 Answers0