serverlesspub / imagemagick-aws-lambda-2

ImageMagick for AWS Lambda 2 runtimes
https://serverless.pub
Other
259 stars 163 forks source link

question on usage in layer #22

Open BryanGerre opened 4 years ago

BryanGerre commented 4 years ago

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,
        });
      });
  });
}; `
anija commented 4 years ago

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 commented 4 years ago

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

anija commented 4 years ago

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 commented 4 years ago

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

bruno-smaldone commented 4 years ago

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

charlie-s commented 4 years ago

@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?

jfederer commented 2 years ago

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

How does this work with imagemagick?