Home  >  Article  >  Web Front-end  >  Nodejs changes image size

Nodejs changes image size

WBOY
WBOYOriginal
2023-05-16 18:48:08877browse

Node.js is a popular JavaScript programming language that is extremely flexible and practical and can be used to develop different kinds of applications on the backend. One such universal application is an online photo gallery and picture editor. In this case, dynamically resizing or scaling images is a very common task. In this article, we will explore how to use Node.js to resize images to better fit our application needs.

  1. Install necessary libraries

Before we start, we need to install some necessary libraries through npm. We can go into the project root directory from the terminal and run the following command:

$ npm install sharp

sharp is an efficient Node.js module that allows us to easily Process images, such as cropping, rotating, scaling, etc.

  1. Implementing the image zoom function

Next, we will write code to implement the image zoom function. Let’s take a look at this simple yet powerful program:

const fs = require('fs');
const sharp = require('sharp');

sharp('input.jpg')
  .resize(200, 200)
  .toFile('output.jpg', (err, info) => {
    if (err) console.log(err);
    console.log(info);
  });

Here we first load the file we want to resize (input.jpg) using the fs module. We then use the resize() method from the sharp module to specify the new image size (here we change the image size to 200x200). Finally, we save the output file in output.jpg.

When we run this program, some information will be printed in the terminal output, which is as follows:

{
  format: 'jpeg',
  width: 200,
  height: 200,
  channels: 3,
  premultiplied: false,
  size: 4370
}

Here, we can see various attributes of the output image, such as format, width , height etc.

  1. Using Node.js to batch process images is more efficient

In actual applications, it may be necessary to adjust the size and size of images in batches. Using Node.js, we can process a large number of images in minimum time. Here is an example to batch resize and resize images:

const fs = require('fs');
const sharp = require('sharp');

const inputFolder = './input/';
const outputFolder = './output/';
const imageSize = 400;

fs.readdir(inputFolder, (err, files) => {
  if (err) console.log(err);

  files.forEach(file => {
    sharp(inputFolder + file)
      .resize(imageSize, imageSize)
      .toFile(outputFolder + 'resized_' + file, (err, info) => {
        if (err) console.log(err);
        console.log(info);
      });
  });
});

Here we first specify the folder we want to resize and the new folder. We then read all the files in that directory and host them into an array of files. Next, we instantiate a sharp object for each file and call the resize() method to specify the new image size. Finally, we save the output file in a new directory and print the output to the terminal.

  1. Image scaling improves application performance

In web applications, dynamically resizing images can help improve application speed and performance. If your application requires frequent loading of large numbers of images in a web browser, it is useful to resize and scale images to fit your layout and style. This makes it possible to transfer only the images you need from the server on demand, rather than downloading larger images. In this case, writing applications using Node.js can help you deliver a high-quality user experience while preserving good web page performance.

Conclusion

In this article, we took a deep dive into how to quickly and easily resize images using Node.js. By using the popular sharp library we can easily resize images in minimal time and increase the speed and performance of our web applications. Now you can easily use these technologies in your applications for a better user experience and better application performance.

The above is the detailed content of Nodejs changes image size. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn