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

Package detail

fastimage

ShogunPanda71.4kISC7.0.1TypeScript support: included

A module that finds the size and type of an image by fetching and reading as little data as needed.

fast, fastimage, image, size, dimensions, resolution, width, height, png, jpeg, bmp, gif, psd, tiff, webp, svg

readme

fastimage

Version Dependencies Build Coverage

A module that finds the size and type of an image by fetching and reading as little data as needed.

http://sw.cowtech.it/fastimage

Installation

Just run:

npm install fastimage

Usage

The signature is fastimage.info(source, [options], [callback]).

The source argument can be:

  • String representing a URL (only http and https protocol are supported).
  • String representing a local file path.
  • Buffer containing image data.
  • Stream containing image data.

The options object supports the following options:

  • threshold: The maximum about of data (in bytes) to downloaded or read before giving up. Default is 4096.
  • timeout: The maximum time (in milliseconds) to wait for a URL to be downloaded before giving up. Default is 30000 (30 s).
  • userAgent: The user agent to use when making HTTP(S) requests. Default is fastimage/$VERSION.

If callback is not provided, the method returns a Promise.

Example

import { info } from 'fastimage'

info('http://fakeimg.pl/1000x1000/', (error, data) => {
  if (error) {
    // ...
  } else {
    // ...
  }
})

const data = await info('http://fakeimg.pl/1000x1000/')

The callback argument (or the resolved value) will be an object with the following properties:

{
  "width": 1000, // The width of the image in pixels.
  "height": 1000, // The height of the image in pixels.
  "type": "png", // The type of the image. Can be one of the supported images formats (see section below).
  "time": 171.43721 // The time required for the operation, in milliseconds.
  "analyzed": 979, // The amount of data transferred (in bytes) or read (in case of files or Buffer) to identify the image.
  "realUrl": "https://fakeimg.pl/1000x1000/", // The final URL of the image after all the redirects. Only present if the source was a URL.
  "size": 17300, // The size of the image (in bytes). Only present if the source was a URL and  if the server returned the "Content-Length" HTTP header.
}

Streams

Calling fastimage.stream it will return a Writable stream which will emit the info event once informations are ready.

The stream accepts only the threshold option.

import { stream } from 'fastimage'

const pipe = createReadStream('/path/to/image.png').pipe(stream({ threshold: 100 }))

pipe.on('info', data => {
  // ...
})

Supported image formats

The supported image type are (thanks to the image-size module):

  • BMP
  • CUR
  • DDS
  • GIF
  • ICNS
  • ICO
  • JPEG
  • KTX
  • PNG
  • PNM (PAM, PBM, PFM, PGM, PPM)
  • PSD
  • SVG
  • TIFF
  • WebP

ESM Only

This package only supports to be directly imported in a ESM context.

For informations on how to use it in a CommonJS context, please check this page.

Contributing to fastimage

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

Copyright (C) 2015 and above Shogun (shogun@cowtech.it).

Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/isc.

changelog

2025-11-27 / 7.0.1

  • fix: Correctly sort type only imports.

2025-11-24 / 7.0.0

2025-11-24 / 6.0.2

  • chore: Updated dependencies.
  • feat!: Dropped support for Node 20.

2024-12-28 / 6.0.1

  • chore: Updated dependencies.
  • chore: Fixed Node.js version in CI.

2024-10-20 / 6.0.0

  • feat!: Dropped support for Node 18 and updated dependencies.

2024-04-12 / 5.0.5

  • chore: Updated dependencies.
  • chore: Added missing dependency.

2024-02-07 / 5.0.4

  • chore: Updated dependencies.
  • chore: Replaced tap with Node test runner.

2024-01-27 / 5.0.3

  • chore: Fixed build.

2024-01-27 / 5.0.2

  • chore: Updated dependencies.

2024-01-24 / 5.0.1

  • chore: Updated TypeScript configuration.

2023-12-20 / 5.0.0

  • chore: Updated dependencies.
  • chore: Changed TypeScript version.

2023-10-23 / 4.1.0

  • chore: Updated dependencies and toolchain.
  • chore: Fixed compilation.
  • chore: CI improvement

2022-11-23 / 4.0.12

  • chore: Updated dependencies.
  • chore: Update package.json
  • fix: Fixed build script.

2022-10-12 / 4.0.11

  • fix: Updated types layout.
  • chore: Updated compilation configuration.

2022-08-30 / 4.0.10

  • chore: Updated dependencies.
  • chore: Remove variadic method.

2022-08-29 / 4.0.9

  • chore: Updated dependencies.
  • fix: Fixed CI.

2022-05-02 / 4.0.8

  • fix: Correctly Export models.

2022-05-02 / 4.0.7

  • fix: Export models.

2022-05-02 / 4.0.6

  • fix: Export models.
  • chore: Use sourcemaps with swc

2022-03-07 / 4.0.5

  • chore: Updated dependencies.

2022-03-07 / 4.0.4

  • chore: Updated dependencies.

2022-01-26 / 4.0.3

  • chore: Updated dependencies and linted code.
  • chore: Updated dependencies.
  • chore: Removed useless file.

2021-11-17 / 4.0.2

2021-11-16 / 4.0.1

  • fix: Added ESM note in the README.md
  • chore: Allow manual CI triggering.
  • chore: Updated badges.
  • fix: Fixed Typescript configuration.

2021-08-24 / 4.0.0

  • feat: Only export as ESM.
  • fix: Removed useless comment.
  • chore: Fine tuned build script.

2021-01-04 / 3.2.0

  • feat: Use different versioning for User-Agent.

2021-01-04 / 3.1.0

  • feat: Export as ESM.

2021-01-03 / 3.0.2

  • chore: Fixed license link in README.md.

2021-01-03 / 3.0.1

  • chore: Updated linter config.
  • chore: Removed IDE files.

2021-01-03 / 3.0.0

  • Simplified to only info and stream methods.
  • Dropped supported for Node < 12.
  • Completely rewritten in TypeScript.

2016-10-29 / 2.0.1

  • Updated request package version to fix a vulnerability. Thanks to @gazay.

2016-03-23 / 2.0.0

  • Dropped support for Node < 5.
  • fastimage.info always includes realPath and realUrl instead of omitting them if equals to path and url.

2016-03-08 / 1.2.0

  • fastimage.threshold can now accept value less than or equal to zero to disable the feature and try to download/open the entire file/stream. This fixes detection of corrupted files.

2016-03-07 / 1.1.1

  • Bumped version in package.json.

2016-03-07 / 1.1.0

  • Added fastimage.userAgent to enable User Agent handling. Thanks to matcarey.

2015-05-25 / 1.0.2

  • Ensured promise is included as dependency.

2015-05-06 / 1.0.1

  • Ensured Node 0.10 compatibility.

2015-03-28 / 1.0.0

  • Renamed fastimage.analyze as fastimage.info.
  • Added fastimage.filteredInfo to filter the result object.
  • Make fastimage.info, fastimage.filteredInfo, fastimage.size and fastimage.type return a Promise.
  • Added fastimage.stream and exported FastImageStream for streaming support.
  • Support for analyzing Buffers.
  • Added examples and test.

2015-03-14 / 0.2.0 - The PI release!

  • Added support for analyzing local files.
  • Added realPath, realUrl and size to the returned objects.
  • Added documentation.
  • Improved README.

2015-03-10 / 0.1.1

  • Export FastImageError as well.

2015-03-10 / 0.1.0

  • Initial version