5

I have the following command:

gm('input.jpg')
.crop(500, 500, 10, 10)
.write('output.jpg', function (err) {
  if (err) {
        console.log(err)
  } else {

        console.log('Success')
  }
})

I'd like to add a condition to it without having to write 2 different commands so it would be something like:

var overlay = true
gm('input.jpg')
.crop(500, 500, 10, 10)
if(overlay == true){
 .draw('image Over 0,0 750,750 overlay.jpg')
}
.write('output.jpg', function (err) {
  if (err) {
        console.log(err)
  } else {

        console.log('Success')
  }
})

I'm know the above code won't run, I'm looking for a suggestion for something that would work, without having 2 different GM commands

Jason Small
  • 1,064
  • 1
  • 13
  • 29
  • One idea might be if there is no overlay, have a transparent PNG lying around and put the name of that as the overlay, so you are always doing the overlay but there may not be anything actually in it. Or maybe I should go get another glass of wine and rethink things... ;-) – Mark Setchell Dec 06 '16 at 16:10

1 Answers1

4

Unless there's something spectacularly magical about how the GM library works here, you can break the gm().crop().write() chain into smaller pieces, à la

var overlay = true;
// ...
var g = gm('input.jpg').crop(500, 500, 10, 10);
if (overlay) {
  g = g.draw('image Over 0,0 750,750 overlay.jpg');
}
g.write('output.jpg', function (err) {
  if (err) throw err;
  console.log('Success');
});
AKX
  • 152,115
  • 15
  • 115
  • 172