In this article we will learn how we can resize an image in node.js
using sharp
package.
We can install the sharp package using the following command:
>npm install sharp
Please Note that sharp
package works only on 64-bit architecture systems.
Suppose we want to resize yellow.png
which is in the same directory as our code. We can perform it in the following way :
//Name of the File : image-resize-sharp.js
const sharp = require('sharp');
const fs = require('fs');
sharp('yellow.png')
.rotate(180)
.resize(200)
.toBuffer()
.then( data => {
fs.writeFileSync('yellow.png', data);
})
.catch( err => {
console.log(err);
});
We can run the code using the following command:
>node image-resize-sharp.js
We have learned how we can resize an image in node.js using sharp
.