15

How to do 'gm composite -gravity center change_image_url base_image_url' with GM Node.js?

How to call gm().command() & gm().in() or gm().out() to achieve the above?

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
Yvonne
  • 153
  • 1
  • 1
  • 3
  • Were you able to figure out how to pass in urls to get composite to work? I'm reading you could only pass in file paths. – NateW Oct 11 '15 at 22:43

6 Answers6

18

After struggling for an hour, here is my solution for your question:

gm composite -gravity center change_image_url base_image_url

gm()
.command("composite") 
.in("-gravity", "center")
.in(change_image_url)
.in(base_image_url)
.write( output_file, function (err) {
  if (!err) 
    console.log(' hooray! ');
  else
    console.log(err);
});

Good luck! Hope it will be helpful to others as well :)

ZeroHackeR
  • 573
  • 1
  • 5
  • 7
11

Install gm, (make sure you already install graphicsmagick

npm install gm

following is my example code to merge two image together (use gm.in)

var gm = require('gm');

gm()
 .in('-page', '+0+0')
 .in('bg.jpg')
 .in('-page', '+10+20') // location of smallIcon.jpg is x,y -> 10, 20
 .in('smallIcon.jpg')
 .mosaic()
 .write('tesOutput.jpg', function (err) {
    if (err) console.log(err);
 });
Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
3

i am doing that this way:

var exec = require('child_process').exec
var command = [
        '-composite',
        '-watermark', '20x50',
        '-gravity', 'center', 
        '-quality', 100,
        'images/watermark.png',
        'images/input.jpg', //input
        'images/watermarked.png'  //output
        ];
         // making watermark through exec - child_process
        exec(command.join(' '), function(err, stdout, stderr) { 
            if (err) console.log(err);

        });
Matiss
  • 5,079
  • 2
  • 27
  • 27
  • 2
    as the active maintainer of gm, i suggest you do it this way... because gm is pretty much unnecessary. only difference is that i would use `execFile` instead of `exec`. – Jonathan Ong Sep 03 '13 at 06:55
  • 3
    in this example where gm is even used?...@Jonathan could you tell more where to use gm. like this `exec('gm composite'+command.join('')..` – Muhammad Umer Apr 25 '14 at 04:09
2

Why does nobody use composite command? (https://github.com/aheckmann/gm)

var gm = require('gm');
var bgImage = 'bg.jpg',
    frontImage = 'front.jpg',
    resultImage = 'result.jpg',
    xy = '+100+150';

gm(bgImage)
  .composite(frontImage)
  .geometry(xy)
  .write(resultImage, function (err) {
    if (!err) console.log('All done');
  });

UPDATE Oh, I watched history of source of this method. It becomes available only on 2014

Rinat
  • 1,941
  • 2
  • 17
  • 26
1

If you want to resize and merge, you can use this:

gm()
.in('-geometry', '+0+0')
.in('./img/img1.png')
.in('-geometry', '300x300+100+200')
.in('./img/img2.png')
.flatten()
.write('resultGM.png', function (err) {
  if (err) console.log(err);
});
Ahmet Cetin
  • 3,683
  • 3
  • 25
  • 34
0

Having the pleasure of being confined to a windows machine for the moment I solved this problem eventually by not using the "gm" module at all. For some reason, even though I have installed graphics-magick via its installer the node module refused to find it in my environment variables. But that could have something to do with the fact that I am trying to make an application with Electron.js (which is similar to Node.js but has its "gotcha's").

var exec = require("child_process").execSync;
var commandArray = [
    __dirname + "/bin/graphicsMagick/gm.exe",    // the relative path to the graphics-magick executable
    "-composite",
    "-gravity",
    "center",
    __dirname + "/data/images/qr/logo-small.png",    // relative paths to the images you want to composite
    __dirname + "/data/images/qr/qr-webpage.png",
    __dirname + "/data/images/qr/qr-webpage-logo.png"    // relative path to the result
];
var returnValue = exec(commandArray.join(" "));

For windows I think this is the correct portable way to do it.

Azmo
  • 144
  • 1
  • 5