Open In App

Node Jimp | scaleToFit

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The scaleToFit() function is an inbuilt function in Nodejs | Jimp that scales the image to the largest size that fits inside the rectangle that has the given width and height.

Syntax: 

scaleToFit(w, h, mode, cb)

Parameter: 

  • w: This parameter stores the width of the image.
  • h: This parameter stores the height of the image.
  • mode:  This is an optional parameter that stores the scaling method.
  • cb: This is an optional parameter that is invoked when compilation is complete.

Input Images: 

Setup Environment : 

npm init -y

Install Dependencies :

npm install jimp 

Example 1:  In this example, we will see the use of scaleToFit().

javascript




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read('
        // scaleToFit Function having width and height
        image.scaleToFit(300, 300)
            .write('scaleToFit1.png');
}
 
main();
console.log('Image Processing Completed');


Output: 


Example 2: With mode and cb (optional parameters) 

javascript




// npm install --save jimp
// import jimp library to the environment
const Jimp = require('jimp');
 
// User-Defined Function to read the images
async function main() {
    const image = await Jimp.read('
        // scaleToFit Function having width, height, mode and callback function
        image.scaleToFit(1000, 1700, Jimp.RESIZE_BEZIER, function (err) {
            if (err) throw err;
        })
            .write('scaleToFit2.png');
}
 
main();
console.log('Image Processing Completed');


Output: 


Reference: https://www.npmjs.com/package/jimp



Similar Reads

Node Jimp | flip
The flip() function is an inbuilt function in Nodejs | Jimp which flips the image horizontally or vertically. The default settings are to flip the image horizontally. Syntax: flip(h, v, cb) Parameter: h: This parameter takes a boolean value, if true the image will be flipped horizontally.v: This parameter takes a boolean value, if true the image wi
2 min read
Node Jimp | Gaussian
The Gaussian() function is an inbuilt function in Nodejs | Jimp that applies a true Gaussian blur to the image but its processing is quite slow in comparison to other Jimp functions. Syntax: gaussian(r, cb) Parameter: r: This parameter stores the pixel radius of the blur.cb: This is an optional parameter that is invoked when compilation is complete
1 min read
Node.js Jimp
Jimp is a node module used to do image processing which is provided by the npm installer. The Jimp - Javascript Image Manipulation Program is a library written entirely in JavaScript for Node, without any external or native dependencies. Various Image manipulation options available in this library are blit, blur, color, contain, and many more.Image
2 min read
Node Jimp | Blit
Introduction The blit() function is an inbuilt function in Nodejs. It is used to combine two bitmap patterns. | Jimp which combines several bitmaps into one using a boolean function. Parameters: src - This parameter stores the source of the image to blit. x - This parameter takes x position to blit the image. y - This parameter takes y position to
2 min read
Node Jimp | Blur
The blur() function is an inbuilt function in Nodejs | Jimp which uses a blur algorithm that produces a similar effect to a Gaussian blur. Syntax: blur(r, cb()) Parameter: r: This parameter stores the radius of the blur.cb: This is an optional parameter that is invoked when compilation is complete. Input Images: Setup Environment: npm init -y Insta
1 min read
Node Jimp | scale
The scale() function is an inbuilt function in Nodejs | Jimp that uniformly scales the image by a factor. Syntax: scale(scale, mode, cb) Parameter: scale: This parameter stores the scaling factor of the image.mode: This is an optional parameter that stores the scaling method.cb: This is an optional parameter that is invoked when compilation is comp
2 min read
Node Jimp | rotate
The rotate() function is an inbuilt function in Nodejs | Jimp that rotates the image clockwise and the dimensions of the image remain the same. Syntax: rotate(r, mode, cb) Parameter: r: This parameter stores the rotation angle for the image.mode: This is an optional parameter that stores the scaling method.cb: This is an optional parameter that is
2 min read
Node Jimp | resize
The resize() function is an inbuilt function in Nodejs | Jimp which resizes the image to a set width and height using a 2-pass bilinear algorithm. Syntax: resize(w, h, mode, cb) Parameter: w: This parameter stores the width of the image.h: This parameter stores the height of the image.mode: This is an optional parameter that stores the scaling meth
2 min read
Node Jimp | normalize
The normalize() function is an inbuilt function in Nodejs | Jimp which normalizes the colors of an image by computing a histogram. Syntax: normalize(cb) Parameter: cb - This is an optional parameter that is invoked when compilation is complete. Input Images: Setup Environment : npm init -yInstall Dependencies : npm install jimp Example 1: In this e
1 min read
Node Jimp | posterize
The posterize() function is an inbuilt function in Nodejs | Jimp that applies a posterize effect with n level. Syntax: posterize(n, cb) Parameter: n: This parameter stores the amount to adjust the contrast and the minimum threshold is twocb: This is an optional parameter that is invoked when compilation is complete. Input Images: Step 1: Setting up
1 min read