Home  >  Article  >  Web Front-end  >  Nodejs gets image rotation angle

Nodejs gets image rotation angle

王林
王林Original
2023-05-13 18:55:07716browse

With the popularity of mobile devices and cameras, we often need to process pictures through programs. Among them, image rotation is a very common requirement. In this article, we will introduce how to get the rotation angle of an image using Node.js.

Preparation
Before you begin, you need to ensure that the Node.js environment has been installed and configured. In addition, we also need to install a Node.js module called "exif-parser", which can be used to read the Exif information of the image.

The method to install the "exif-parser" module is as follows:

npm install exif-parser
Method to obtain the image rotation angle
Before understanding how to obtain the image rotation angle, we You must first understand some knowledge about Exif. Exif is metadata stored in pictures taken by digital cameras, including camera model, shooting time, ISO value, aperture and other information. In Exif, there is an attribute called "Orientation", which records the orientation information of the image.

Among them, the value of "Orientation" can be the following numbers:
1: Indicates that the image has not been rotated at all, and the default direction of the image is upward at this time
2: Indicates that the image is flipped horizontally
3: Indicates that the image is rotated 180 degrees counterclockwise
4: Indicates that the image is flipped vertically
5: Indicates that the image is rotated 90 degrees clockwise and flipped horizontally
6: Indicates that the image is rotated 90 degrees clockwise
7 : Indicates that the image is rotated 90 degrees counterclockwise and flipped horizontally
8: Indicates that the image is rotated 90 degrees counterclockwise

With this information, it becomes very simple to obtain the rotation angle of the image. We only need to read the "Orientation" attribute value in the Exif information and determine the rotation angle of the image based on different values.

The following is the code to obtain the image rotation angle:

//Introducing the "exif-parser" module
const ExifParser = require('exif-parser');
// Introduce the file reading module
const fs = require('fs');

// Define the image path
const imagePath = './example.jpg';

/ / Read image files
const imageBuffer = fs.readFileSync(imagePath);

// Parse Exif information
const exifParser = ExifParser.create(imageBuffer);
const exifResult = exifParser. parse();

// Get the Orientation attribute value
const orientation = exifResult.tags.Orientation;

// Determine the rotation angle of the image based on different values
let degree = 0;
switch (orientation) {
case 3:

degree = 180;
break;

case 6:

degree = 90;
break;

case 8:

degree = 270;
break;

}

console.log(The image rotation angle is ${degree} degrees);

In the above code, we first read a picture named "example.jpg" image, and parsed the Exif information through the "exif-parser" module. Then, the rotation angle of the image is determined based on the value of "Orientation". Finally, the rotation angle of the image is printed.

Summary
This article introduces how to use Node.js to obtain the rotation angle of an image. We determine the rotation angle of the image by reading the "Orientation" attribute value in the Exif information. In this way, we can process images more conveniently and improve our development efficiency.

The above is the detailed content of Nodejs gets image rotation angle. 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