Yvonne
Yvonne

Reputation: 153

How to do composite with gm node.js?

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?

Upvotes: 15

Views: 13443

Answers (6)

Ahmet Cetin
Ahmet Cetin

Reputation: 3823

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

Upvotes: 1

Rinat
Rinat

Reputation: 2021

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

Upvotes: 2

Azmo
Azmo

Reputation: 144

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.

Upvotes: 0

ZeroHackeR
ZeroHackeR

Reputation: 573

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

Upvotes: 18

Ryan W
Ryan W

Reputation: 6173

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

Upvotes: 11

Matiss
Matiss

Reputation: 5349

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);

        });

Upvotes: 3

Related Questions