Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-image-size

andreyk151223.6kMIT2.4.0TypeScript support: included

Functional for getting dimensions of any image by url

react, react-hooks, image, image-size, image-dimension, dimension, size, resolution, image-resolution, width, height

readme

react-image-size

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:

`typescript jsx import { useImageSize } from 'react-image-size';

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

if (loading) { return

Loading...
; }

if (error) { return

Error: {error}
; }

return (

Width: {dimensions?.width}

Height: {dimensions?.height}

); }

You can also use the getImageSize function directly:
```typescript
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<Dimensions>

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);

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

2.4.0 - 2024-06-14

Changed

  • Reset error when url change

2.3.2 - 2023-11-02

Changed

  • Move constants

2.3.1 - 2023-11-02

Changed

  • Improve README.md

2.3.0 - 2023-11-02

Added

  • Add react and react-dom to dev dependencies

Changed

  • Rename Changes.md to CHANGELOG.md and Readme.md to README.md

Removed

2.2.0 - 2023-11-02

Added

  • Add object with error constants

2.1.0 - 2023-11-02

Added

Changed

  • Improve tslint
  • Improve tslint.json configuration
  • Update react and react-dom peer dependencies to >=16.9.0 version

2.0.2 - 2023-03-16

Changed

Fixed

2.0.1 - 2023-03-14

Changed

Fixed

  • Fix example with "reject timer" in the README.md (PR #6)

2.0.0 - 2022-11-26

Added

Changed

1.0.6 - 2022-11-26

Changed

  • Update reactImageSize response object

Fixed

  • Fix Next.js issue (Issue #3)

1.0.5 - 2022-04-30

Changed

  • Move reactImageSize function to index.js file
  • Update package.json type, files and exports

1.0.4 - 2020-11-16

Added

  • Add error message to reject argument

1.0.3 - 2020-11-15

Added

1.0.2 - 2020-11-15

Fixed

  • Fix default export

1.0.1 - 2020-11-15

Changed

  • Change export type
  • Rename timeout variable

1.0.0 - 2020-11-15

Added