Node.js 如何在AWS Lambda函数中使用ImageMagick`drawtext`方法?
我目前正在运行一个Node 8 Lambda函数,使用Node gm模块()提供的ImageMagick 7对一些图像处理进行注释。我正在使用的其他ImageMagick函数(如Node.js 如何在AWS Lambda函数中使用ImageMagick`drawtext`方法?,node.js,aws-lambda,imagemagick,drawtext,Node.js,Aws Lambda,Imagemagick,Drawtext,我目前正在运行一个Node 8 Lambda函数,使用Node gm模块()提供的ImageMagick 7对一些图像处理进行注释。我正在使用的其他ImageMagick函数(如resize()或quality())工作正常;但是,使用drawText()不会出错,也不会导致在输出回s3的图像上绘制任何文本。我还没有找到关于在Lambda中使用drawText()的更多信息,我很好奇是否有其他人使用Node或其他方法成功地做到了这一点。任何第一手经验或见解都将不胜感激 我怀疑这与字体(或缺少字体
resize()
或quality()
)工作正常;但是,使用drawText()
不会出错,也不会导致在输出回s3的图像上绘制任何文本。我还没有找到关于在Lambda中使用drawText()
的更多信息,我很好奇是否有其他人使用Node或其他方法成功地做到了这一点。任何第一手经验或见解都将不胜感激
我怀疑这与字体(或缺少字体)有关,因此尝试将我自己的字体
添加到etc/ImageMagick-7/type.xml
中的ImageMagicktype.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}`);
…在相关位的末尾,处理后的图像将发送回s3ImageMagick是否可以使用Gotham字体?如果没有,请尝试Gotham字体文件本身的路径。我也有同样的情况,使用字体路径而不是名称。对此有什么解决方案吗?对于我们来说,在节点8中工作,而不是在节点10或12中工作?我也是。