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.
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.
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.
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.
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!