我目前正在使用Node gm模块(https://github.com/aheckmann/gm)出现的ImageMagick 7运行Node 8 Lambda函数,以注释一些图像处理。我正在使用的其他ImageMagick函数(例如resize()
或quality()
)可以正常工作;但是,使用drawText()
不会出错,也不会导致在输出回s3的图像上绘制任何文本。我无法在Lambda中找到有关使用drawText()
的大量信息,并且很好奇是否有人成功使用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"/>
字体添加到位于{{1}的ImageMagick type.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;
...相关位的末尾,然后将处理后的图像发送回s3