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

react-image-size

v2.3.2

Published

Functional for getting dimensions of any image by url

Downloads

24,976

Readme

npm type definitions npm npm package minimized gzipped size (select exports) NPM npm

Introduction

react-image-size is a JavaScript library for obtaining the width and height of an image from its URL. It provides a React hook and an asynchronous function for retrieving image dimensions.

Installation

You can install the library using npm or yarn:

npm install -S react-image-size

or

yarn add react-image-size

Usage

To use the library in your React project, you can import the useImageSize hook and call it with the image URL:

import { useImageSize } from 'react-image-size';

function App() {
  const [dimensions, { loading, error }] = useImageSize('https://example.com/image.jpg');

  if (loading) {
    return <div>Loading...</div>;
  }

  if (error) {
    return <div>Error: {error}</div>;
  }

  return (
    <div>
      <p>Width: {dimensions?.width}</p>
      <p>Height: {dimensions?.height}</p>
    </div>
  );
}

You can also use the getImageSize function directly:

import { getImageSize } from 'react-image-size';

async function fetchImageSize() {
    try {
        const dimensions = await getImageSize('https://example.com/image.jpg');
        console.log(dimensions);
    } catch (error) {
        console.error(error);
    }
}

API Reference

The library exports two functions and two types:

useImageSize(url: string, options?: Options): UseImageSizeResult

A React hook that returns the dimensions of the image and a loading and error state. Parameters:

  • url: the URL of the image
  • options: an optional object with the following properties:
    • timeout: the maximum time in milliseconds to wait for the image to load before rejecting the Promise. Default is undefined.

Returns an array with the following elements:

  • dimensions: an object with the width and height of the image, or null if the image has not yet loaded.
  • state: an object with the following properties:
    • loading: a boolean indicating whether the image is currently loading.
    • error: a string containing an error message, or null if no error occurred.

getImageSize(url: string, options?: Options): Promise

An asynchronous function that returns a Promise that resolves to an object with the width and height of the image. Parameters:

  • url: the URL of the image
  • options: an optional object with the following properties:
    • timeout: the maximum time in milliseconds to wait for the image to load before rejecting the Promise. Default is undefined.

Returns a Promise that resolves to an object with the following properties:

  • width: the width of the image
  • height: the height of the image

Options

An object with the following optional properties:

  • timeout: the maximum time in milliseconds to wait for the image to load before rejecting the Promise. Default is undefined.

Dimensions

An object with the following properties:

  • width: the width of the image
  • height: the height of the image

Conclusion

react-image-size is a lightweight and easy-to-use library for retrieving the dimensions of an image from its URL. With both a React hook and an asynchronous function available, it can be integrated seamlessly into any project.

Migrate from V1 to V2

OLD: import reactImageSize from 'react-image-size';
NEW: import { getImageSize } from 'react-image-size';

OLD: const { width, height } = await reactImageSize(imageUrl);
NEW: const { width, height } = await getImageSize(imageUrl);