Node Resize Large Images to Small Size with Sharp Tutorial

When we talk about image resizing in Node, comprehensive packages are available that can help you resize an image in Node with minimal configuration and time.

Recently, I was assigned a task where I had to transform the large image size into a smaller size.

As usual, I started searching for better options; after putting in some time, I learned about the sharp package.

In this tutorial, I would like to share my experience with you on how I learned to resize the image in the node js application using the sharp library.

Sharp is a small and one of the most powerful image processing plugin for the node js environment. You can convert the large image size to a small size, and it allows you to transform an image in the most notable file formats. JPEG, PNG, WebP, and AVIF

How to Optimize Image Size with Any Image Format in Node Js

  • Step 1: Build New Folder
  • Step 2: Create Package.json File
  • Step 3: Make New App File
  • Step 4: Install Sharp Package
  • Step 5: Resize Image in Node
  • Step 6: Test Node Application

Build New Folder

In this step, you are creating a blank folder, this will be your node project directory.

You can either create folder manually or take help of below command.

mkdir node-backend

Let’s head over to the project’s root:

cd node-backend

Create Package.json File

In the next step, we will again use the following command, but this time we will be using the command to create a new package.json file.

npm init

Install Sharp Package

As we said earlier, we will convert the image size using an external dependency.

Consequently, we again have to type and run the command to install the sharp module.

npm install sharp

Make Server File

In order to build the promised functionality we need a main file where we can write the code.

Therefore, make the new server.js file at the root of your node project.

You have to now register the server.js file in the package.json file’s script section.

  "scripts": {
    "start": "node server.js"
  },

Resize Image in Node

Import the sharp module, and this incorporation allows access to sharp methods.

These methods will help us transform the image size in node.

The sharp() method takes the file name that needs to be resized, defines the other methods that can help you customize the image along with resizing.

The toFile() method keeps the resized image in your favorite directory.

Insert suggested code in server.js file.

const sharp = require('sharp')

sharp('./uploads/james-webb.jpg')
  .rotate()
  .resize(200)
  .jpeg({ mozjpeg: true })
  .toFile('./uploads/img/resized.jpeg', (err, info) => {
    console.log('Image has been resized.')
  })

Test Node Application

You must be waiting for the resize process to start, head over to terminal.

Thereafter, type the given command onto the console and hit enter.

node server.js

After the process is completed a success message appear on console screen.

Image has been resized.

Now you may check the uploads/img directory to see the results.

Node Resize Large Images to Small Size with Sharp Tutorial

Summary

In this quick tutorial, you have learned about image resizing in node js on the fly. We comprehended how to set up a node project from scratch and install and configure sharp dependency.

Most importantly, we saw how to rotate the image after changing the image dimensions in the node.