Page 1 of 1

New to ImageMagick AWS NodeJS Lambda .drawTexte

Posted: 2020-01-18T10:22:27-07:00
by bryaninva
I was able to install ImageMagick as a layer and can currently resize, and add shapes, but I'm unable to drawText. I'm following this https://tech.mybuilder.com/memes-as-a-s ... agemagick/ as pasted below.

Again I do get the image back from the response, but it's missing the drawText words on it of "hello" Any guidance would be appreciated

Code: Select all

const gm = require('gm').subClass({ imageMagick: true });
const fs = require('fs');

const { IMAGES_DIR, TEXT_SIZE, TEXT_PADDING } = process.env;

const parseText = text => (text || '').toUpperCase();
const getImages = () => fs.readdirSync(IMAGES_DIR);
const parseImage = image => getImages().find(file => file.indexOf(image) === 0);
const random = arr => arr[Math.floor(Math.random() * arr.length)];
const randomImage = () => random(getImages());


module.exports.meme = (event, context, callback) => {
  const input = event.queryStringParameters || {};

  const top = parseText('hello'); 
  const image = parseImage(input.image) || randomImage();

  const meme = gm(`${IMAGES_DIR}${image}`);

  meme.size(function (err, { height }) {
    meme
      .font('Courier', 20)
      .fill('white')
      .stroke('black', 2)
      .drawText(0, 0, top, 'center') 
      .toBuffer(function (err, buffer) {
        callback(null, {
          statusCode: 200,
          headers: { 'Content-Type': 'image/jpeg' },
          body: buffer.toString('base64'),
          isBase64Encoded: true,
        });
      });
  });
};