Fast, lightweight NodeJS package to get dimensions of any image file or buffer
npx @tessl/cli install tessl/npm-image-size@2.0.0Image Size is a fast, lightweight NodeJS package that extracts dimensions from image files and buffers without loading the entire image into memory. It provides zero-dependency image dimension detection for over 20 image formats with minimal memory footprint.
npm install image-sizeimport imageSize, { disableTypes, types } from "image-size";
// or named imports only
import { imageSize, disableTypes, types } from "image-size";
import { imageSizeFromFile, setConcurrency } from "image-size/fromFile";For CommonJS:
const imageSize = require("image-size"); // default export
// or destructured
const { imageSize, disableTypes, types } = require("image-size");
const { imageSizeFromFile, setConcurrency } = require("image-size/fromFile");import { imageSize } from "image-size";
import { imageSizeFromFile } from "image-size/fromFile";
import { readFileSync } from "node:fs";
// From a buffer (synchronous)
const buffer = readFileSync("photo.jpg");
const dimensions = imageSize(buffer);
console.log(dimensions.width, dimensions.height); // 1920 1080
// From a file (asynchronous - recommended)
const fileDimensions = await imageSizeFromFile("photo.jpg");
console.log(fileDimensions.width, fileDimensions.height); // 1920 1080
// Access all available sizes for multi-size formats (ICO, CUR, HEIF)
const { images } = await imageSizeFromFile("icon.ico");
images?.forEach(img => console.log(`${img.width}x${img.height}`));Image Size is built around several key components:
BMP, CUR, DDS, GIF, HEIC (HEIF, AVCI, AVIF), ICNS, ICO, J2C, JPEG-2000 (JP2), JPEG, JPEG-XL, KTX (1 and 2), PNG, PNM (PAM, PBM, PFM, PGM, PPM), PSD, SVG, TGA, TIFF, WebP
Synchronous dimension extraction from image buffers and Uint8Array data. Perfect for streams, network requests, or when you already have image data in memory.
function imageSize(input: Uint8Array): ISizeCalculationResult;
function disableTypes(types: imageType[]): void;
const types: imageType[];Asynchronous dimension extraction directly from image files with configurable concurrency control. Recommended for local file operations.
function imageSizeFromFile(filePath: string): Promise<ISizeCalculationResult>;
function setConcurrency(c: number): void;interface ISizeCalculationResult {
width: number;
height: number;
orientation?: number;
type?: string;
images?: ISize[];
}
interface ISize {
width: number;
height: number;
orientation?: number;
type?: string;
}
type imageType = 'bmp' | 'cur' | 'dds' | 'gif' | 'heif' | 'icns' | 'ico' | 'j2c' | 'jp2' | 'jpg' | 'jxl' | 'jxl-stream' | 'ktx' | 'png' | 'pnm' | 'psd' | 'svg' | 'tga' | 'tiff' | 'webp';
// Note: imageType represents format detection values. The result.type in ISizeCalculationResult
// may contain more specific format strings (e.g., 'avif', 'heic' when imageType is 'heif')The package includes a CLI tool for quick image dimension inspection:
# Using npx (recommended)
npx image-size photo.jpg icon.png favicon.ico
# Global installation
npm install -g image-size
image-size *.jpg
# Example output:
# 1920x1080 - photo.jpg (jpg)
# 256x256 - icon.png (png)
# 16x16 - favicon.ico (ico)
# 32x32 - favicon.ico (ico)