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

probe-image-size

v7.2.3

Published

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

Downloads

2,125,738

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.