What is gm?
The gm (GraphicsMagick) npm package is a Node.js wrapper for the GraphicsMagick and ImageMagick image processing libraries. It allows you to perform a wide range of image manipulation tasks such as resizing, cropping, rotating, and adding text to images.
What are gm's main functionalities?
Image Resizing
This feature allows you to resize an image to the specified dimensions. In this example, the image is resized to 240x240 pixels.
const gm = require('gm');
gm('/path/to/image.jpg')
.resize(240, 240)
.write('/path/to/resized_image.jpg', function (err) {
if (!err) console.log('Image resized successfully');
});
Image Cropping
This feature allows you to crop an image to the specified width and height, starting from the specified x and y coordinates. In this example, a 100x100 pixel area is cropped from the image starting at coordinates (50, 50).
const gm = require('gm');
gm('/path/to/image.jpg')
.crop(100, 100, 50, 50)
.write('/path/to/cropped_image.jpg', function (err) {
if (!err) console.log('Image cropped successfully');
});
Image Rotation
This feature allows you to rotate an image by a specified angle. In this example, the image is rotated by 45 degrees with a green background fill.
const gm = require('gm');
gm('/path/to/image.jpg')
.rotate('green', 45)
.write('/path/to/rotated_image.jpg', function (err) {
if (!err) console.log('Image rotated successfully');
});
Adding Text to Image
This feature allows you to add text to an image. In this example, the text 'Hello World' is added to the image at coordinates (30, 50) with a font size of 68.
const gm = require('gm');
gm('/path/to/image.jpg')
.fontSize(68)
.drawText(30, 50, 'Hello World')
.write('/path/to/text_image.jpg', function (err) {
if (!err) console.log('Text added to image successfully');
});
Other packages similar to gm
sharp
Sharp is a high-performance image processing library for Node.js, which is based on the libvips library. It is known for its speed and low memory usage compared to gm. Sharp supports a wide range of image formats and provides functionalities such as resizing, cropping, rotating, and more.
jimp
Jimp is a pure JavaScript image processing library for Node.js with no external dependencies. It is easy to use and provides a wide range of image manipulation functionalities such as resizing, cropping, rotating, and adding text. Jimp is suitable for smaller projects or environments where installing external binaries is not feasible.
lwip
LWIP (Lightweight Image Processor) is a lightweight image processing library for Node.js. It provides functionalities such as resizing, cropping, rotating, and color manipulation. LWIP is designed to be fast and efficient, but it has fewer features compared to gm and sharp.
gm
GraphicsMagick for node
var gm = require('./gm')
// resize and remove EXIF profile data
gm('/path/to/my/img.jpg')
.resize(240, 240)
.noProfile()
.write('/path/to/resize.png', function(err){
if (!err) print('done')
})
// obtain the size of an image
gm('/path/to/my/img.jpg')
.size(function(err, size){
if (!err)
print( size.width > size.height ? 'wider' : 'taller than you' )
})
// output all available image properties
gm('/path/to/img.png')
.identify(function(err, data){
if (!err) console.dir(data)
})
// pull out the first frame of an animated gif and save as png
gm('/path/to/animated.gif[0]')
.write('/path/to/firstframe.png', function(err){
if (err) print('aaw, shucks')
})
// crazytown
gm('/path/to/my/img.jpg')
.flip()
.magnify()
.rotate('green', 45)
.blur(7, 3)
.crop(300, 300, 150, 130)
.edge(3)
.write('/path/to/crazy.jpg', function(err){
if (!err) print('crazytown has arrived')
})
// annotate an image with some graphics
gm('/path/to/my/img.jpg')
.stroke("#ffffff")
.drawCircle(10, 10, 20, 10)
.font("Helvetica.ttf", 12)
.drawText(30, 20, "GMagick!")
.write("/path/to/drawing.png", function(err){
if (!err) print('done')
})
Getting started
First download and install GraphicsMagick
then either use npm:
npm install gm
or clone the repo:
git clone git://github.com/aheckmann/gm.git
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.
Methods
Node version
Compatible with v0.1.96+g
Inspiration
http://github.com/quiiver/magickal-node
License
(The MIT License)
Copyright (c) 2010 Aaron Heckmann
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.