Resize Images

Resize image using sharp module of node.js

30 Days of Node | Node.js Tutorial series



Overview

In this article we will learn how we can resize an image in node.js using sharp package.

Installation

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.

Image Resizing

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);
    });												
								
							

Run

We can run the code using the following command:

								
>node image-resize-sharp.js											
								
							

Summary

We have learned how we can resize an image in node.js using sharp.