node.jsaws-lambdaimagemagickgraphicsmagickgm

Nodejs gm library - how to create convert command for multiple images to be layered in sequence


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?


Solution

  • 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)
        }
    })