Search code examples
node.jsimage-processinggraphicsmagick

Convert and resize image to png using gm


I want to switch from Image Magick to Graphics Magick. The following code converts an image to PNG:

var ImageMagick = require("imagemagick");

// convert the image
ImageMagick.convert([
    "input.jpg"
  , '-resize'
  , "200x100"
  , "output.png"
], function(err, stdout){
    if (err) { throw err; }
    console.log(">> Done");
});

How can I do the same but using Graphics Magick?


Solution

  • Using resize and write methods:

    var Gm = require("gm");
    
    Gm("input.jpg").resize(200, 100, "!").write("output.png", function (err) {
        if (err) throw err;
        console.log('image converted.');
    });
    

    Note you have to install Graphics Magick binaries:

    sudo apt-get install graphicsmagick
    brew install graphicsmagick