如何在AWS Lambda函数中使用ImageMagick“drawtext”方法?

2024-04-22

我目前正在使用 Node gm 模块显示的 ImageMagick 7 运行 Node 8 Lambda 函数(https://github.com/aeckmann/gm https://github.com/aheckmann/gm)来注释一些图像处理。其他 ImageMagick 函数(例如resize() or quality())我正确使用工作;然而使用drawText()不会出错,但也不会导致在输出回 s3 的图像上绘制任何文本。我找不到太多有关使用的信息drawText()在 Lambda 中,我很好奇是否有其他人使用 Node 或其他方式成功地做到了这一点。任何第一手经验或见解将不胜感激。

我怀疑这与字体(或缺乏字体)有关,因此尝试添加我自己的字体<type name="Gotham" fullname="Gotham-Book" family="Gotham" weight="400" style="normal" stretch="normal" glyphs="/var/task/imagemagick/etc/ImageMagick-7/Gotham-Book.ttf"/>到 ImageMagicktype.xml文件位于etc/ImageMagick-7/type.xml.

const aws = require('aws-sdk');
const im = require('gm').subClass({
    imageMagick: true,
    appPath: '/var/task/imagemagick/bin/',
});
const s3 = new aws.S3();

const IMAGEMAGICK_PATH = `${process.env.LAMBDA_TASK_ROOT}/imagemagick/bin/`;
const INPUT_SOURCE_BUCKET = 'input';
const OUTPUT_SOURCE_BUCKET = 'output';

exports.handler = (event, context) => {
    process.env.PATH = `${process.env.PATH}:${IMAGEMAGICK_PATH}`;

    console.log(`** Event Received ** ${event.Records[0].Sns.Message}`);
    const message = JSON.parse(event.Records[0].Sns.Message);

    const inputKey = decodeURIComponent(message.Records[0].s3.object.key.replace(/\+/g, ' '));
    console.log(`** Image Input Key ** ${inputKey}`);

    // This splits the input key on the `/`, creates an array of strings,
    // and pulls the last string from that array.
    const filename = inputKey.split('/')[3].split('.')[0];

    let channel;
    let imageBuffer;
    let imageType;
    let outputKeyPath;
    let shootId;

    console.log('** Download beginning **');
    // Download the image from S3 into a buffer.
    const getParams = {
        Bucket: INPUT_SOURCE_BUCKET,
        Key: inputKey
    };
    s3.getObject(getParams, (err, response) => {
        if (err) {
            console.log('err :', err);
            return err;
        }
        imageBuffer = response.Body;

...暂停分配相关变量等,然后...

        console.log(imageBuffer, filename);
        im(imageBuffer)
            .antialias(true)
            // Remove profile data http://aheckmann.github.io/gm/docs.html#profile.
            .noProfile()
            .resize(830)
            // Adjusts the jpeg|miff|png|tiff compression level, values
            // range from 0 to 100.
            .quality(85)
            .fill('white')
            .font('Gotham', 24)
            .drawText(4, 7, 'image text', 'SouthWest')
            .toBuffer('PNG', (err, buffer) => {
                if (err) {
                    console.error(`** error - convert - toBuffer - ${err} **`);
                    return err;
                }
                const Key = `${outputKeyPath}_full.png`;
                console.log(`** Output Path ** ${Key}`);

...相关位结束,处理后的图像然后发送回 s3


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在AWS Lambda函数中使用ImageMagick“drawtext”方法? 的相关文章

随机推荐