npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get-image-orientation

v0.0.4

Published

Utility for determining image orientation and aspect ratio

Downloads

99

Readme

Installation

Run npm install get-image-orientation --save

Usage

import getImageOrientation from 'get-image-orientation';

const image = {
    width: 320,
    height: 240
};
const {
    aspect, // number/int/float
    isPortrait, // boolean
    isLandscape, // boolean
    isSquare, // boolean
    widthIncrease, // number/int
    heightIncrease, // number/int
    isSquareLikePortrait, // boolean
    isSquareLikeLandscape // boolean
    ratio // string
} = getImageOrientation(image.width, image.height, 10);

Syntax

getImageOrientation(320, 240, 10);

Parameters

  • width - image width, REQUIRED

  • height - image height, REQUIRED

  • maxIncrease - max width/height increase in percentage, default = 15. maxIncrease is used for determining isSquareLikePortrait and isSquareLikeLandscape flags.

API

getImageOrientation returns the following fields:

aspect

Number (int/float). This value represents the aspect of the image. It refers to the image height, when that image has a landscape orientation. When the image has a portrait orientation, it refers to its width. Aspect is calculated by the formula image.width / image.height.

isPortrait

Boolean. Determines if the image has a portrait orientation. An image has a portrait orientation, when image.width > image.height.

isLandscape

Boolean. Determines if the image has a landscape orientation. An image has a landscape orientation, when image.width < image.height.

isSquare

Boolean. Determines if the image has a square orientation. An image has a square orientation, when image.width === image.height.

widthIncrease

Number (int). This value represents the width increase, compared to image.height in percentage, eg ((width - height) * 100) / width. Width increase is calculated when isLandscape is true. Otherwise, it returns 0.

heightIncrease

Number (int). This value represents the height increase, compared to image.width in percentage, eg ((height - width) * 100) / height. Height increase is calculated when isPortrait is true. Otherwise, it returns 0.

isSquareLikePortrait

Boolean. Determines if the image has a square-like portrait orientation. An image has a square-like portrait orientation, when that image isPortrait and its heightIncrease is less than or equal to maxIncrease. In other words, this check returns true for near-square portrait images, with height just a bit bigger than their width. This check is useful for differentiating standard portrait images from square-like images.

isSquareLikeLandscape

Boolean. Determines if the image has a square-like landscape orientation. An image has a square-like landscape orientation, when that image isLandscape and its widthIncrease is less than or equal to maxIncrease. This check returns true for near-square landscape images, with width just a bit bigger than their height. This check is useful for differentiating standard landscape images from square-like images.

ratio

String. Represents the closest matching aspect ratio for the image. Available ratios are:

  • 2/3
  • 1/1
  • 4/3
  • 16/10
  • 5/3
  • 16/9
  • 21/9

Tests

const orientation = getImageOrientation(1600, 900);

// orientation contains:
{
    aspect: 1.78,
    isPortrait: false,
    isLandscape: true,
    isSquare: false,
    isSquareLikePortrait: false,
    isSquareLikeLandscape: false,
    widthIncrease: 43,
    heightIncrease: 0,
    ratio: '16/9'
}
const orientation = getImageOrientation(322, 480);

// orientation contains:
{
    aspect: 0.67,
    isPortrait: true,
    isLandscape: false,
    isSquare: false,
    isSquareLikePortrait: false,
    isSquareLikeLandscape: false,
    widthIncrease: 0,
    heightIncrease: 32,
    ratio: '2/3'
}
const orientation = getImageOrientation(555, 480);

// orientation contains:
{
    aspect: 1.16,
    isPortrait: false,
    isLandscape: true,
    isSquare: false,
    isSquareLikePortrait: false,
    isSquareLikeLandscape: true,
    widthIncrease: 13,
    heightIncrease: 0,
    ratio: '1/1'
}
const orientation = getImageOrientation(413, 480);

// orientation contains:
{
    aspect: 0.86,
    isPortrait: true,
    isLandscape: false,
    isSquare: false,
    isSquareLikePortrait: true,
    isSquareLikeLandscape: false,
    widthIncrease: 0,
    heightIncrease: 13,
    ratio: '1/1'
}
const orientation = getImageOrientation(480, 480);

// orientation contains:
{
    aspect: 1,
    isPortrait: false,
    isLandscape: false,
    isSquare: true,
    isSquareLikePortrait: false,
    isSquareLikeLandscape: false,
    widthIncrease: 0
    heightIncrease: 0,
    ratio: '1/1'
}