Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gm

> 编程语言
Open source

GraphicsMagick for node

7.0K stars0 likes0 views
WebsiteGitHub

About

GraphicsMagick for node

2025-02-24 This project is not maintained

Instead of using this project, execute the gm or magick binaries using cross-spawn directly.

Nearly 15 years ago I started this project as part of my start up which I sold later that year (2010). Having not used this project in over a decade and with no contributors for years, it's time to officially sunset gm.

No further Issues will be addressed. No Pull Requests will be merged. No new commits or npm releases will be made.


Massive thank you to everyone who contributed to this project over the years.


I want to continue using gm. What do I do?

All past gm releases published to the npm registry will continue to be available for install. However, you should prioritize moving off of this project to an alternative because the risk of unpatched vulnerabilities in this project will continue to increase over time. No new commits will land and no new releases will be published.

The most obvious alternative to gm I see is installing cross-spawn and executing the GraphicsMagick or ImageMagick binaries directly, after all, that's pretty much all this project did. There may be other gm alternatives on npm but I don't what they are offhand so you'll need to search for something suitable yourself.


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.

Getting started

First download and install GraphicsMagick or ImageMagick. In Mac OS X, you can simply use Homebrew and do:

brew install imagemagick
brew install graphicsmagick

then either use npm:

npm install gm

or clone the repo:

git clone git://github.com/aheckmann/gm.git

Use ImageMagick instead of gm

Subclass gm to enable ImageMagick 7+

const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: '7+' });

Or, to enable ImageMagick legacy mode (for ImageMagick version < 7)

const fs = require('fs')
const gm = require('gm').subClass({ imageMagick: true });

Specify the executable path

Optionally specify the path to the executable.

const fs = require('fs')
const gm = require('gm').subClass({
  appPath: String.raw`C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe`
});

Basic Usage

…

Streams

…

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) return handle(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) return handle(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:

  1. command - ie convert
  2. in - the input arguments
  3. source - stdin or an image file
  4. out - the output arguments
  5. 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,

gm('img.png').format(function (err, format) {

})

// is equivalent to

gm('img.png').identify('%m', function (err, format) {

})

since %m is the format option for getting the image file format.

Platform differences

Please document and refer to any platform or ImageMagick/GraphicsMagick issues/differences here.

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.

    1. gm(path) When you pass a string as the first argument it is interpreted as the path to an image you intend to manipulate.
    1. gm(stream || buffer, [filename]) You may also pass a ReadableStream or Buffer as the first argument, with an optional file name for format inference.
    1. 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

Methods

  • getters

    • size - returns the size (WxH) of the image
    • orientation - returns the EXIF orientation of the image
    • format - returns the image format (gif, jpeg, png, etc)
    • depth - returns the image color depth
    • color - returns the number of colors
    • res - returns the image resolution
    • filesize - returns image filesize
    • identify - returns all image data available. Takes an optional format string.
  • manipulation

    • adjoin
    • affine
    • antialias
    • append
    • authenticate
    • autoOrient
    • average
    • backdrop
    • bitdepth
    • blackThreshold
    • bluePrimary
    • blur
    • border
    • borderColor
    • box
    • channel
    • charcoal
    • chop
    • clip
    • coalesce
    • colors
    • colorize
    • colorMap
    • colorspace
    • comment
    • compose
    • compress
    • contrast
    • convolve
    • createDirectories
    • crop
    • cycle
    • deconstruct
    • delay
    • define
    • density
    • despeckle
    • dither
    • displace
    • display
    • dispose
    • dissolve
    • edge
    • emboss
    • encoding
    • enhance
    • endian
    • equalize
    • extent
    • file
    • filter
    • flatten
    • flip
    • flop
    • foreground
    • frame
    • fuzz
    • gamma
    • gaussian
    • geometry
    • gravity
    • greenPrimary
    • highlightColor
    • highlightStyle
    • iconGeometry
    • implode
    • intent
    • interlace
    • label
    • lat
    • level
    • list
    • [limit](http://aheckmann.github.io/gm/

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •gm().command() - Custom command such as identify or convert
  • •gm().in() - Custom input arguments
  • •gm().out() - Custom output arguments
  • •1) gm(path) When you pass a string as the first argument it is interpreted as the path to an image you intend to manipulate.
  • •2) gm(stream || buffer, [filename]) You may also pass a ReadableStream or Buffer as the first argument, with an optional file name for format inference.
  • •size - returns the size (WxH) of the image
  • •orientation - returns the EXIF orientation of the image
  • •format - returns the image format (gif, jpeg, png, etc)
  • •depth - returns the image color depth
  • •color - returns the number of colors

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言