Skip to main content

In this article, we will explore different approaches to checking the orientation of an image file in Node.js. Specifically, we will compare two popular libraries, "sharp" and "image-size".

Sharp

To check if an image file is vertical or horizontal in Node.js, you can utilize the `sharp` package, which is a popular image processing library. Here's an example of how you can determine the orientation of an image file using `sharp`:

First, you need to install the `sharp` package by running the following command in your Node.js project directory:

npm install sharp

Once you have `sharp` installed, you can use the following code to check the orientation of an image file:

const sharp = require('sharp');

function checkImageOrientation(imagePath) {
  return sharp(imagePath)
    .metadata()
    .then(metadata => {
      const { width, height } = metadata;
      if (width > height) {
        return 'horizontal';
      } else if (height > width) {
        return 'vertical';
      } else {
        return 'square';
      }
    })
    .catch(err => {
      console.error('Error reading image metadata:', err);
      return null;
    });
}

// Usage example
const imagePath = 'path/to/your/image.jpg';

checkImageOrientation(imagePath)
  .then(orientation => {
    console.log('Image orientation:', orientation);
  })
  .catch(err => {
    console.error('Error checking image orientation:', err);
  });

In the code above, we define the `checkImageOrientation` function that takes the path to an image file as a parameter. The function uses `sharp` to read the image's metadata, including the width and height. It then compares the dimensions to determine whether the image is horizontal, vertical, or square. The function returns a Promise that resolves with the orientation string.

You can replace `'path/to/your/image.jpg'` with the actual path to your image file, and the code will output the image's orientation (either 'horizontal', 'vertical', or 'square') to the console.

Make sure to replace `'path/to/your/image.jpg'` with the actual path to your image file.

Image-size

Another library is `image-size`. It provides a simple and lightweight solution for reading image dimensions without requiring any external dependencies. Here's an example of how you can use `image-size` to determine the orientation of an image file:

First, install the `image-size` package by running the following command in your Node.js project directory:

npm install image-size

Once you have `image-size` installed, you can use the following code to check the orientation of an image file:`

const sizeOf = require('image-size');

function checkImageOrientation(imagePath) {
  try {
    const dimensions = sizeOf(imagePath);
    const { width, height } = dimensions;
    if (width > height) {
      return 'horizontal';
    } else if (height > width) {
      return 'vertical';
    } else {
      return 'square';
    }
  } catch (err) {
    console.error('Error reading image dimensions:', err);
    return null;
  }
}

// Usage example
const imagePath = 'path/to/your/image.jpg';

const orientation = checkImageOrientation(imagePath);
console.log('Image orientation:', orientation);

In the code above, we define the `checkImageOrientation` function that takes the path to an image file as a parameter. It uses `image-size` to read the dimensions of the image and then compares the width and height to determine the orientation. The function returns a string indicating whether the image is horizontal, vertical, or square.

You can replace `'path/to/your/image.jpg'` with the actual path to your image file, and the code will output the image's orientation to the console.

Note that unlike `sharp`, `image-size` does not provide metadata beyond the dimensions, so if you require additional image processing capabilities, such as resizing or cropping, `sharp` might be a more suitable choice.