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

Package detail

probe-image-size

nodeca1.7mMIT7.2.3TypeScript support: definitely-typed

Get image size without full download (JPG, GIF, PNG, WebP, BMP, TIFF, PSD)

image, size, jpg, jpeg, ico, gif, png, webp, tiff, bmp, svg, psd

readme

probe-image-size

CI NPM version Coverage Status

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO, AVIF, HEIC, HEIF.

Key features:

  • small size, no heavy dependencies
  • works with remote and local data
  • effective with big images (speed/memory), download minimal data from remotes
  • extracts orientation value when available
  • easy to browserify (splitted to components)

Install

npm install probe-image-size

Example

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

// Get by URL
let result = await probe('http://example.com/image.jpg');
console.log(result); // =>
/*
  {
    width: xx,
    height: yy,
    type: 'jpg',
    mime: 'image/jpeg',
    wUnits: 'px',
    hUnits: 'px',
    url: 'http://example.com/image.jpg'
  }
*/


// By URL with options
let result = await probe('http://example.com/image.jpg', { rejectUnauthorized: false });
console.log(result);


// From the stream
let result = await probe(require('fs').createReadStream('image.jpg'));
console.log(result);


// From a Buffer (sync)
let data = require('fs').readFileSync('image.jpg');
console.log(probe.sync(data));

API

Note:

  • You can access/browserify stream.js / http.js / sync.js directly.
  • If you don't like http.js dependencies, you can create your own wrapper for stream.js.

probe(src [, options|keepOpen]) -> Promise

  • src can be of this types:
    • String - URL to fetch
    • Stream - readable stream
  • options - HTTP only. See needle documentation, and customized defaults.
  • keepOpen (Boolean) - stream only. Keep stream open after parser finishes (input stream will be closed by default)

result (Promise) contains:

{
  width: XX,
  height: YY,
  length: ZZ,   // byte length of the file (if available, HTTP only)
  type: ...,    // image 'type' (usual file name extention)
  mime: ...,    // mime type
  wUnits: 'px', // width units type ('px' by default, can be different for SVG)
  hUnits: 'px', // height units type ('px' by default, can be different for SVG)
  url: ...,     // HTTP only, last url for the image in chain of redirects
                // (if no redirects, same as src)

  // optional, image orientation (from Exif), number from 1 to 8;
  // you may wish to swap width and height if orientation is >= 5
  orientation: X,

  // optional, full list of sizes for ICO (always) and AVIF (if multiple images)
  variants: [ { width, height }, ... ] | undefined
}

Width and height in the output object represent image size before any transformations (orientation, cropping) are applied. Orientation is returned separately, which you may wish to apply afterwards depending on browser support (browsers only support JPEG orientation for now). See known issues for details.

Returned errors can be extended with 2 fields:

  • code - equals to ECONTENT if the library failed to parse the file;
  • status - equals to a HTTP status code if it receives a non-200 response.

probe.sync(src) -> result|null

Sync version can eat arrays, typed arrays and buffers. On success it returns the same result as async version. On fail it returns null.

Note. Formats like JPEG & TIFF can store size anywhere (far from the head). That usually does not happens, but if you need guarantees - always provide full file content to sync methods. We strongly recommend to use async version as memory-friendly.

Similar projects

Support probe-image-size

You can support this project via Tidelift subscription.

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.

7.2.3 - 2022-01-27

Fixed

  • Fix Error extend in FF, #71.

7.2.2 - 2021-10-12

Fixed

  • Skip invalid chunks in JPEG parser instead of stopping, so some broken images can still be parsed, #68.
  • Better error handling when server redirects to utf8-encoded internationalized domain name.

7.2.1 - 2021-06-07

Fixed

  • Fix hanging of HTTP request when server returns JSON or XML, #62.

7.2.0 - 2021-06-03

Changed

  • SVG embedded in HTML is no longer recognized as SVG image, #60.

Fixed

  • Allow XML namespaces in SVG images, #61.

7.1.1 - 2021-05-27

Fixed

  • Allow byte order mark at the start of SVG, #57.

7.1.0 - 2021-04-15

Fixed

  • Fix options merge, when property value is class (switched from deepmerge to lodash.merge), #53.

7.0.1 - 2021-03-18

Fixed

  • Fix crash in async SVG parser, #52.

7.0.0 - 2021-03-11

Added

  • Add AVIF/HEIC/HEIF support.
  • Add orientation info.

6.0.0 - 2020-11-04

Added

  • Add support for .ico files.

Changed

  • node.js v10+ required.
  • Drop callbacks support.
  • Drop legacy call support (URL in options).
  • Input stream will now be closed by default.
  • Use needle instead of outdated request (options names are different).
  • Rewrite tests to async/await.
  • Deps bump & cleanup.

5.0.0 - 2019-09-14

Changed

  • Drop any-promise, use native, #32.

4.1.1 - 2019-07-11

Fixed

  • Fix streams unpipe (after 4.1.0 changes), #34.

4.1.0 - 2019-07-09

Changed

  • Deps bump.
  • Update Travis-CI node versions to actual.

Fixed

  • Fix content ungzip from misconfigured servers, #31.

4.0.1 - 2019-07-08

Fixed

  • Fix regexp to ignore SVG stroke-width attr, #33.

4.0.0 - 2018-03-05

Changed

  • Roll back got -> request, see #16.
  • Default timeout 30s -> 60s.

Fixed

  • Fix padding parse in jpeg, #20.

3.2.0 - 2017-11-22

Changed

  • Roll back got to 6.x due serious regressions, see #16. Next attempt will be switching to request and releasing 4.0.0.

3.1.0 - 2017-06-08

Changed

  • Maintenance, deps bump. got 6.x -> 7.x. got timeouts may work a bit different but should affect result.

3.0.0 - 2016-12-02

Changed

  • Rewrite internals to Promise.
  • Separate options from url for http probe (old signature still supported for compatibility).
  • err.status -> err.statusCode
  • remove { rejectUnauthorized: false } from defaults.
  • User-Agent string update.
  • Replaced request dependency with got (read options description in doc).
  • Retry requests on network fail.
  • Switched from readable-stream to native stream (node 4+ has normal Stream3).
  • Proper class for errors.

2.2.0 - 2016-10-26

Added

  • Add .url with actual image address (after redirects) for remotes.

2.1.1 - 2016-08-25

Added

  • Add default user agent to http requests (if not set by options), #8.

2.1.0 - 2016-07-14

Changed

  • Internal parsers api cleanup - switch from callbacks to events.

Fixed

  • Fixed "write after end" error under heavy load.

2.0.1 - 2016-07-01

Fixed

  • Fixed bug in streams cleanup condition.

2.0.0 - 2016-06-25

Added

  • SVG support
  • Return dimention units ('px' everywhere except SVG)

    Changed

  • width/height now can be float (with fractional part)

1.2.1 - 2016-05-30

Fixed

  • Stream: posponed callback to avoid possible races on forced stream close.

1.2.0 - 2016-05-28

Added

  • Added .sync.probe() method.
  • 100% tests coverage.

Changed

  • Splited to separate files (simplify browserification).
  • Faster return on positive result & faster resource release.

Fixed

  • Fix stream error handling.

1.1.0 - 2016-05-25

Added

  • Added promise support.

Changed

  • Use readable-stream instead of stream.
  • Reorganised internal files structure & tests.

1.0.6 - 2016-04-13

Fixed

  • Fixed parser crashes on zero length data & offsets.

1.0.5 - 2015-12-15

Changed

  • Increased http request timeout to 30 seconds.
  • Don't check SSL sertificates.

1.0.4 - 2015-09-22

Fixed

  • Fixed crash on empty JPEG markers.

1.0.3 - 2015-09-19

Fixed

  • Fixed catch internal exceptions from request.

1.0.2 - 2015-09-16

Added

  • Added ECONTENT error code for parse errors.

1.0.1 - 2015-09-14

Added

  • Return image length when possible.
  • Support URLs in dev helper script.

1.0.0 - 2015-09-12

Added

  • First release.