New to ImageMagick AWS NodeJS Lambda .drawTexte

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
bryaninva
Posts: 1
Joined: 2020-01-18T10:10:31-07:00
Authentication code: 1152

New to ImageMagick AWS NodeJS Lambda .drawTexte

Post 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,
        });
      });
  });
};

Post Reply