GraphicsMagick Image Manipulation For Nodejs

Posted By : Sakshi Gadia | 31-May-2018

To manipulate images like cropped, compressed or resized etc. on the server side, we can use gm node module.

It can be accessed by:

npm install gm

 

To crop images:

Use gm module it takes width, height parameter to crop images

 

const gm = require('gm');
gm('/path/to/image').crop(300,400) // width, height
.write('/path/to/output', (err) => {
if(err){
console.log(err)
}
console.log(done)
})
 

 

To resize images:

gm('/path/to/image').resize(width, null)

The above code will resize the width by maintaining the aspect ratio.

Same as to resize the height

 
gm('/path/to/image').resize(null, height)

 

To compress images:

Compress type: None, BZip, Fax, Group4, JPEG, Lossless, LZW, RLE, Zip, or LZMA

 
gm('path/to/image').compress(type)

 

To use images by url, use request npm:

 
var request = require('request');
var url = "www.abc.com/pic.jpg"

gm(request(url))
.write('/path/to/image', function (err) {
  if (!err) console.log('done');
});

 

Note:

There are various method of gm module

You can see the link for more details

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

About Author

Author Image
Sakshi Gadia

An experienced MEAN Stack developer having good knowledge in Nodejs, MongoDb. Apart from these in my spare time, I enjoy playing chess and ready to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..