imagemagick-aws-lambda-2 icon indicating copy to clipboard operation
imagemagick-aws-lambda-2 copied to clipboard

question on usage in layer

Open BryanGerre opened this issue 5 years ago • 7 comments

I've added the layer using the link ServerlessPub link and clicked deploy which added a layer named image-magick I then attached the layer to my lambda. In my lambda I have const gm = require('gm').subClass({imageMagick: true});

Is that using the layer? For some reason I'm unable to see any words on my image using drawText

here is my code in full

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

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 bottom = parseText('mem');
  const image = parseImage(input.image) || randomImage();
 

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

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

BryanGerre avatar Jan 18 '20 22:01 BryanGerre

I've the same problem: everything is fine, resize and other functions works fine, but the drawText just don't work and doesn't raise any error/warning.

anija avatar Feb 14 '20 10:02 anija

Here's the solution: https://github.com/serverlesspub/imagemagick-aws-lambda-2/issues/6

anija avatar Feb 14 '20 11:02 anija

The compiled build in https://github.com/serverlesspub/imagemagick-aws-lambda-2/issues/6 is not working for me (buffer is always empty). I've tried to build the library from 0, without changing anything from the repo, and the result is the same: buffer is always empty. I'm building with MacOs.

Any chance to get a precompiled and working layer zip with freetype?

anija avatar Feb 14 '20 17:02 anija

Solved! I was finally able tu build with freetype enabled. Here's the Layer .zip to be uplaoded on AWS ;) Archivio.zip

anija avatar Feb 17 '20 11:02 anija

@anija - This one worked for me, thank you very much for sharing it, you saved me a ton of work

bruno-smaldone avatar Mar 02 '20 20:03 bruno-smaldone

@anija Any chance we can get a hand with this? Uploaded Archivio.zip as a new layer, linked the layer to our simple Lambda function, and now code that worked with imagemagick-aws-lambda-2 is no longer working – produces Stream yields empty buffer output.

Here's our Lambda function with the extraneous bits removed, but this is the exact gm() call that we're making with this new layer:

const gm = require('gm').subClass({ imageMagick: true });
...
gm(path.join(__dirname, '/file.jpg'))
    .toBuffer('jpeg', (err, buffer) => {
        if (err) return console.error(err); <-- this throws.
        console.log(buffer)
    });

Is there anything else we need to do to use / link this layer and its binaries properly?

charlie-s avatar Mar 06 '20 18:03 charlie-s

@anija I'm not following. The .zip that you posted appears to be HarfBuzz OpenType text engine.

How does this work with imagemagick?

jfederer avatar Feb 19 '22 04:02 jfederer