1

What is the equivalent nodejs gm library https://github.com/aheckmann/gm command to this imagemagick cli command?

imagemagick cli command to layer several images on a transparent background:

convert -size 669x122 xc:none img1.jpg -geometry +223+0 -composite 
        img2.jpg -geometry +251+46 -composite 
        img3.png -geometry +283+46 -composite 
        img4.jpg -geometry +446+61 -composite 
        img5.jpg -geometry +223+61 -composite 
        img6.jpg -geometry +0+61 -composite 
        output.png

gm library command would be:?

const gm = require('gm').subClass({
    imageMagick: true // im binaries are already installed on lambda functions
})

gm()
.out('-size 669x122 xc:none 
       img1.jpg -geometry +223+0 -composite 
       img2.jpg -geometry +251+46 -composite 
       img3.png -geometry +283+46 -composite 
       img4.jpg -geometry +446+61 -composite 
       img5.jpg -geometry +223+61 -composite 
       img6.jpg -geometry +0+61 -composite 
       output.png')
.write()

I'm new to nodejs and this will be running on an aws lambda function. Imagemagick binaries are preinstalled on lambda. In addition to my initial question, should I just use the exec() nodejs functionality to pass in this string or is there a benefit to using nodejs gm library?

Zelf
  • 1,723
  • 2
  • 23
  • 40
  • You have to add lots of braces and parentheses and dots to do anything in `node`... https://stackoverflow.com/q/37586684/2836621 – Mark Setchell Sep 24 '18 at 10:33
  • Yes, but how would I loop through an array of images with x/y coordinates to create the gm call with the dots? In docs I see: "If gm does not supply you with a method you need or does not work as you'd like, you can simply use gm().in() or gm().out() to set your own arguments." I guess I'll do some testing, but was hoping someone knowledgeable with gm might be able to steer me a little faster. – Zelf Sep 24 '18 at 14:31

1 Answers1

0

Technically, not the answer to my original question, but another way to solve the problem. Ended up abandoning gm npm library and just using exec(). Still would like to know if .out would have worked or was right though.

Here's part of nodejs codebase for creating an image with multiple layers using imagemagick.

const exec = require('child_process').exec

let command = []

for (let i = 0; i < my['images'].length; i++) {
    if (i === 0) {
        command.push('convert')
        command.push(`-size ${my['canvas_width']}x${my['canvas_height']} xc:none`)
    }

    command.push(`${local}${my['images'][i]['image']}`)
    command.push(`-geometry +${my['images'][i]['x']}+${my['images'][i]['y']}`)
    command.push('-composite')
}

command.push(`${local}${outputImage}`)
command = command.join(' ')

console.log(command)

exec(command, (err, stdout, stderr) => {
    if (err) {
        next(`${err} ${stdout} ${stderr}`)
    } else {
        next(null)
    }
})
Zelf
  • 1,723
  • 2
  • 23
  • 40