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
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.