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, PSD.
Install
npm install probe-image-size --save
Example
var probe = require('probe-image-size');
probe('http://example.com/image.jpg', function (err, result) {
console.log(result);
});
probe({ url: 'http://example.com/image.jpg', timeout: 5000 }, function (err, result) {
console.log(result);
});
probe('http://example.com/image.jpg').then(function (result) {
console.log(result);
});
var input = require('fs').createReadStream('image.jpg');
probe(input, function (err, result) {
console.log(result);
input.destroy();
});
API
probe(src [, callback(err, result)])
src
can be of this types:
- String - URL to fetch
- Object - options for request,
defaults are
{ timeout: 5000, maxRedirects: 2 }
- Stream - readable stream
result
contains:
{
width: XX,
height: YY,
length: ZZ,
type: ...,
mime: ...
}
err
is an error, which is extended with:
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.
If callback not provided, Promise
will be returned.
Note. If you use stream as source, it's your responsibility to terminate
reading in callback. That will release resources as soon as possible. On
http requests that's done automatically.
Similar projects
License
MIT