What is probe-image-size?
The probe-image-size npm package is used to quickly and efficiently determine the dimensions and type of an image by reading only a small portion of the file. This can be particularly useful for applications that need to handle images but want to avoid loading entire files into memory.
What are probe-image-size's main functionalities?
Get Image Dimensions
This feature allows you to get the dimensions of an image by reading a stream of the image file. The code sample demonstrates how to use the probe-image-size package to read an image file from the filesystem and log its dimensions.
const probe = require('probe-image-size');
const fs = require('fs');
async function getImageDimensions(filePath) {
const stream = fs.createReadStream(filePath);
const result = await probe(stream);
console.log(result);
}
getImageDimensions('path/to/image.jpg');
Get Image Type
This feature allows you to determine the type of an image (e.g., JPEG, PNG, GIF) by reading a stream of the image file. The code sample demonstrates how to use the probe-image-size package to read an image file from the filesystem and log its type.
const probe = require('probe-image-size');
const fs = require('fs');
async function getImageType(filePath) {
const stream = fs.createReadStream(filePath);
const result = await probe(stream);
console.log(result.type);
}
getImageType('path/to/image.jpg');
Get Image Dimensions from URL
This feature allows you to get the dimensions of an image from a URL by fetching the image and reading its stream. The code sample demonstrates how to use the probe-image-size package to fetch an image from a URL and log its dimensions.
const probe = require('probe-image-size');
const fetch = require('node-fetch');
async function getImageDimensionsFromURL(url) {
const response = await fetch(url);
const result = await probe(response.body);
console.log(result);
}
getImageDimensionsFromURL('https://example.com/image.jpg');
Other packages similar to probe-image-size
image-size
The image-size package is another popular library for determining the dimensions of an image. Unlike probe-image-size, image-size reads the entire file into memory, which can be less efficient for large images. However, it supports a wide range of image formats and is straightforward to use.
sharp
Sharp is a high-performance image processing library that can also be used to get image metadata, including dimensions. While it offers more extensive image manipulation capabilities compared to probe-image-size, it is also more complex and has a larger footprint.
jimp
Jimp is a JavaScript image processing library that can read image dimensions and perform various image manipulations. It is written entirely in JavaScript and does not require native dependencies, making it easier to install and use in some environments. However, it may not be as performant as probe-image-size for simply probing image dimensions.
probe-image-size
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');
let result = await probe('http://example.com/image.jpg');
console.log(result);
let result = await probe('http://example.com/image.jpg', { rejectUnauthorized: false });
console.log(result);
let result = await probe(require('fs').createReadStream('image.jpg'));
console.log(result);
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,
type: ...,
mime: ...,
wUnits: 'px',
hUnits: 'px',
url: ...,
orientation: X,
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.