image-meta
Detect image type and size using pure JavaScript. It has no dependencies and works in Node.js, browsers and other runtimes.
Usage
import { imageMeta } from "image-meta";
const res = await fetch(url);
const data = new Uint8Array(await res.arrayBuffer());
const meta = imageMeta(data);
// => { type: "png", width: 123, height: 456 }
In Node.js, a Buffer works as input because it is a Uint8Array:
import { readFile } from "node:fs/promises";
import { imageMeta } from "image-meta";
const meta = imageMeta(await readFile("./image.jpg"));
The type is detected from the file contents, not the file extension.
Result
type ImageMeta = {
type?: string; // detected format, e.g. "png"
width: number; // of the largest image when there are several
height: number;
orientation?: number; // EXIF orientation (jpg)
images?: Omit<ImageMeta, "images">[]; // all embedded images (ico, cur, icns)
};
Error handling
imageMeta throws an error if the input is not a Uint8Array, the format is unsupported, or the data is invalid. Wrap calls that take untrusted input in try/catch:
try {
const meta = imageMeta(data);
} catch {
// Not a supported image
}
Supported formats
avif, bmp, cur, dds, gif, heic, icns, ico, j2c, jp2, jpg, jxl, ktx, png, pnm, psd, svg, tga, tiff, webp
Development
- Clone this repository
- Install the latest LTS version of Node.js
- Enable Corepack using
corepack enable - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
See AGENTS.md for the project layout and how to add a new format.
License
Made with
Based on image-size by Aditya Yadav and contributors.
Published under MIT License.