image-sizer
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Image-Sizer

CI NPM version Coverage Status

Get image size from the raw image. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.

Inspiration

I wanted a library that could take an image binary within the browser and calculate the natural width and height of the image without rendering it on the DOM. There are currently a few other libraries that do this but they have to be used with patch-package due to their nodejs dependencies. This library is a fork of probe-image-size but is primarily intended for browser use but maintains full compatibility with node. The project has also been updated to typescript and the API has been simplified for the more focused purpose.

Install

npm install image-sizer

Example

import sizer from 'image-sizer'

let res = await fetch('http://example.com/image.jpg');
let data = await res.arrayBuffer()
let sizeInfo = sizer(data)
console.log(sizeInfo); // =>
/*
  {
    width: xx,
    height: yy,
    type: 'jpg',
    mime: 'image/jpeg',
    wUnits: 'px',
    hUnits: 'px',
  }
*/

// If you know the type of image you can run sizer with an extension
let sizeInfo = sizer(data,'jpeg')
console.log(sizeInfo); // =>
/*
  {
    width: xx,
    height: yy,
    type: 'jpg',
    mime: 'image/jpeg',
    wUnits: 'px',
    hUnits: 'px',
  }
*/

Similar projects

Package Sidebar

Install

npm i image-sizer

Weekly Downloads

7

Version

1.0.2

License

MIT

Unpacked Size

46.2 kB

Total Files

54

Last publish

Collaborators

  • mfish33