How to do composite with gm node.js?

前端 未结 6 1237
情话喂你
情话喂你 2020-12-24 08:22

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 <

相关标签:
6条回答
  • 2020-12-24 09:00

    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.

    0 讨论(0)
  • 2020-12-24 09:19

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

    0 讨论(0)
  • 2020-12-24 09:20

    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

    0 讨论(0)
  • 2020-12-24 09:21

    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);
    
            });
    
    0 讨论(0)
  • 2020-12-24 09:22

    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);
     });
    
    0 讨论(0)
  • 2020-12-24 09:23

    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);
    });
    
    0 讨论(0)
提交回复
热议问题