0

I'm trying to pass the convert arguments as string to in() without success.

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. ie "event.command"

1 Answers1

0

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.

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. 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:

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

farukcankaya
  • 544
  • 7
  • 11