Fast, lightweight NodeJS package to get dimensions of any image file or buffer
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Synchronous dimension extraction from image buffers and Uint8Array data. This approach is ideal for streams, network requests, memory-based operations, or when you already have image data loaded.
Extracts dimensions from image buffer synchronously with automatic format detection.
/**
* Extract dimensions from image buffer
* @param input - Image data as Uint8Array or Buffer
* @returns Dimension data with width, height, type, and optional metadata
* @throws TypeError if unsupported file type or disabled type
*/
function imageSize(input: Uint8Array): ISizeCalculationResult;
interface ISizeCalculationResult {
/** Image width in pixels */
width: number;
/** Image height in pixels */
height: number;
/** JPEG EXIF orientation (1-8) if present */
orientation?: number;
/** Detected image format string */
type?: string;
/** Array of all sizes for multi-size formats (ICO, CUR, HEIF) */
images?: ISize[];
}
interface ISize {
width: number;
height: number;
orientation?: number;
type?: string;
}Usage Examples:
import imageSize from "image-size"; // default export
// or
import { imageSize } from "image-size"; // named export
import { readFileSync } from "node:fs";
// Basic usage with file buffer
const buffer = readFileSync("photo.jpg");
const dimensions = imageSize(buffer);
console.log(`${dimensions.width}x${dimensions.height}`); // 1920x1080
console.log(`Format: ${dimensions.type}`); // Format: jpg
// Handle JPEG with orientation
const jpegBuffer = readFileSync("rotated-photo.jpg");
const jpegInfo = imageSize(jpegBuffer);
if (jpegInfo.orientation) {
console.log(`Orientation: ${jpegInfo.orientation}`); // 1-8
}
// Multi-size formats (ICO, CUR, HEIF)
const icoBuffer = readFileSync("favicon.ico");
const icoInfo = imageSize(icoBuffer);
console.log(`Main size: ${icoInfo.width}x${icoInfo.height}`); // Largest image
if (icoInfo.images) {
console.log("All sizes:");
icoInfo.images.forEach(img =>
console.log(` ${img.width}x${img.height}`)
);
}
// Handle network requests
import fetch from 'node-fetch';
const response = await fetch('https://example.com/image.png');
const arrayBuffer = await response.arrayBuffer();
const imageBuffer = new Uint8Array(arrayBuffer);
const netDimensions = imageSize(imageBuffer);
// Handle streams
import { Transform } from 'node:stream';
const imageStream = new Transform({
transform(chunk, encoding, callback) {
try {
const dimensions = imageSize(chunk);
console.log(`Stream image: ${dimensions.width}x${dimensions.height}`);
callback(null, chunk);
} catch (error) {
// Might need more data for complete header
callback(null, chunk);
}
}
});Control which image formats are processed for performance optimization.
/**
* Globally disable specific image format detectors
* @param types - Array of image format strings to disable
* @returns void
*/
function disableTypes(types: imageType[]): void;
/**
* Array of all supported image format strings
*/
const types: imageType[];
type imageType = 'bmp' | 'cur' | 'dds' | 'gif' | 'heif' | 'icns' | 'ico' | 'j2c' | 'jp2' | 'jpg' | 'jxl' | 'jxl-stream' | 'ktx' | 'png' | 'pnm' | 'psd' | 'svg' | 'tga' | 'tiff' | 'webp';Usage Examples:
import { disableTypes, types, imageSize } from "image-size";
import { readFileSync } from "node:fs";
// View all supported formats
console.log("Supported formats:", types);
// ['bmp', 'cur', 'dds', 'gif', 'heif', 'icns', 'ico', ...]
// Disable rarely used formats for performance
disableTypes(['tiff', 'psd', 'dds']);
// This will now throw TypeError for disabled formats
try {
const tiffBuffer = readFileSync("document.tiff");
const dimensions = imageSize(tiffBuffer);
} catch (error) {
console.log(error.message); // "disabled file type: tiff"
}
// Common optimization: disable all but web formats
disableTypes(['bmp', 'cur', 'dds', 'icns', 'ico', 'j2c', 'jp2', 'jxl', 'jxl-stream', 'ktx', 'pnm', 'psd', 'tga', 'tiff']);
// Now only supports: gif, heif, jpg, png, svg, webpThe buffer processing functions can throw the following errors:
// Unsupported format
try {
const result = imageSize(unknownBuffer);
} catch (error) {
console.log(error.message); // "unsupported file type: undefined"
}
// Disabled format
disableTypes(['png']);
try {
const result = imageSize(pngBuffer);
} catch (error) {
console.log(error.message); // "disabled file type: png"
}