[英]gm imagemagick passing in convert arguments as a string
I'm trying to pass the convert arguments as string to in() without success.我试图将转换 arguments 作为字符串传递给 in() 但没有成功。
works fine if I run this:如果我运行这个,工作正常:
const GM = require('gm');
const gm = GM.subClass({ imageMagick: true });
gm(buf).command('convert').in('-resize','800x','-resize','800x','-sharpen','0x.4','-crop','160x180+0+0','+repage','-quality','92').toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
But not if I run:但如果我跑:
const GM = require('gm');
const gm = GM.subClass({ imageMagick: true });
let command = "'-resize','800x','-resize','800x','-sharpen','0x.4','-crop','160x180+0+0','+repage','-quality','92'";
gm(buf).command('convert').in(command).toBuffer((err, buffer) => err ? reject(err) : resolve(buffer));
seems to just ignore the command altogether.似乎完全忽略了命令。
The reason I'm attempting to pass the command as a string is that I ultimately want to pass the command to lambda node as an event.我尝试将命令作为字符串传递的原因是我最终想将命令作为事件传递给 lambda 节点。 ie "event.command"
即“event.command”
I've faced the same problem recently.我最近遇到了同样的问题。 aws-lambda-image is used to process images on the Lambda function. It and the underlying ImageMagick library do not support every capability of ImageMagick. In GraphicMagic for node, you have to build a chain of operators as shown in this example .
aws-lambda-image用于处理 Lambda function 上的图像。它和底层 ImageMagick 库不支持 ImageMagick 的所有功能。在节点的 GraphicMagic 中,您必须构建一个运算符链,如本例所示。
However, as in your situation, we may have very complex queries and we don't want to add a new operator for every different query.但是,在您的情况下,我们可能有非常复杂的查询,我们不想为每个不同的查询添加一个新的运算符。 Therefore, I wrote a converter that converts raw imagemagick
convert
query into GM's chain of operators.因此,我编写了一个转换器,将原始 imagemagick
convert
查询转换为 GM 的运算符链。 It works like below:它的工作原理如下:
// Initiate GM
let img = gm(image).limit("memory", `${getEnableMemory()}MB`);
let imageToConvert = img.command("convert");
// Generate operators
let operationAndParameters = parseArgsStringToArgv(command);
let operatorIndexes = findIndexOfOperators(operationAndParameters);
let operators = generateOperators(operatorIndexes, operationAndParameters);
// Apply operators to the image dynamically
operators.forEach(o => imageToConvert.out(o.operator, ...o.parameters));
You can also create ImageConverter like ImageResizer to use in your aws-lambda-image function. In this case, only you need to do is:您还可以像ImageResizer这样创建ImageConverter以在您的aws-lambda-image function 中使用。在这种情况下,您只需要做的就是:
let command = "'-resize','800x','-resize','800x','-sharpen','0x.4','-crop','160x180+0+0','+repage','-quality','92'";
const converter = new ImageConverter(params)
converter.exec(image)
Check my gist to see the script: https://gist.github.com/farukcankaya/5b698044d907fe80a7c1f6e3afe618bc检查我的要点以查看脚本: https://gist.github.com/farukcankaya/5b698044d907fe80a7c1f6e3afe618bc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.