gm

GraphicsMagick and ImageMagick for node

Bug Reports

When reporting bugs please include the version of graphicsmagick/imagemagick you're using (gm -version/convert -version) as well as the version of this module and copies of any images you're having problems with.

// you also need to convert (write() or stream()) the image afterwards

// NOTE: this buffers the readStream in memory!

var readStream =fs.createReadStream('/path/to/my/img.jpg');

gm(readStream)

.size({bufferStream:true},function(err,size){

this.resize(size.width/2,size.height/2)

this.write('/path/to/resized.jpg',function(err){

if(!err)console.log('done');

});

});

Buffers

// A buffer can be passed instead of a filepath as well

var buf =require('fs').readFileSync('/path/to/image.jpg');

gm(buf,'image.jpg')

.noise('laplacian')

.write('/path/to/out.jpg',function(err){

if(err)returnhandle(err);

console.log('Created an image from a Buffer!');

});

/*

A buffer can also be returned instead of a stream

The first argument to toBuffer is optional, it specifies the image format

*/

gm('img.jpg')

.resize(100,100)

.toBuffer('PNG',function(err,buffer){

if(err)returnhandle(err);

console.log('done!');

})

Custom Arguments

If gm does not supply you with a method you need or does not work as you'd like, you can simply use gm().in() or gm().out() to set your own arguments.

gm().command() - Custom command such as identify or convert

gm().in() - Custom input arguments

gm().out() - Custom output arguments

The command will be formatted in the following order:

command - ie convert

in - the input arguments

source - stdin or an image file

out - the output arguments

output - stdout or the image file to write to

For example, suppose you want the following command:

gm "convert""label:Offline""PNG:-"

However, using gm().label() may not work as intended for you:

gm()

.label('Offline')

.stream();

would yield:

gm "convert""-label""\"Offline\"""PNG:-"

Instead, you can use gm().out():

gm()

.out('label:Offline')

.stream();

which correctly yields:

gm "convert""label:Offline""PNG:-"

Custom Identify Format String

When identifying an image, you may want to use a custom formatting string instead of using -verbose, which is quite slow.
You can use your own formatting string when using gm().identify(format, callback).
For example,

Platform differences

Examples:

Check out the examples directory to play around.
Also take a look at the extending gm
page to see how to customize gm to your own needs.

Constructor:

There are a few ways you can use the gm image constructor.

gm(path) When you pass a string as the first argument it is interpreted as the path to an image you intend to manipulate.

gm(stream || buffer, [filename]) You may also pass a ReadableStream or Buffer as the first argument, with an optional file name for format inference.

gm(width, height, [color]) When you pass two integer arguments, gm will create a new image on the fly with the provided dimensions and an optional background color. And you can still chain just like you do with pre-existing images too. See here for an example.

The links below refer to an older version of gm but everything should still work, if anyone feels like updating them please make a PR

composite

GraphicsMagick supports compositing one image on top of another. This is exposed through gm.composite(). Its first argument is an image path with the changes to the base image, and an optional mask image.

Currently, gm.composite() only accepts file paths.

gm.composite(other [, mask])

gm('/path/to/image.jpg')

.composite('/path/to/second_image.jpg')

.geometry('+100+150')

.write('/path/to/composite.png',function(err){

if(!err)console.log("Written composite image.");

});

montage

GraphicsMagick supports montage for combining images side by side. This is exposed through gm.montage(). Its only argument is an image path with the changes to the base image.

Currently, gm.montage() only accepts file paths.

gm.montage(other)

gm('/path/to/image.jpg')

.montage('/path/to/second_image.jpg')

.geometry('+100+150')

.write('/path/to/montage.png',function(err){

if(!err)console.log("Written montage image.");

});

Contributors

Inspiration

Plugins

License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.