Image Meta Detector

0.2.2 · active · verified Sun Apr 19

image-meta is a lightweight, pure JavaScript library designed for detecting the type and dimensions (width, height, orientation) of various image formats directly from a `Buffer` or `Uint8Array`. It supports common formats like JPEG, PNG, GIF, BMP, WebP, SVG, ICO, AVIF, HEIC, and TIFF. The package is currently at version 0.2.2 and appears to have a relatively active, though not rapid, release cadence, with updates for new image formats like AVIF and HEIC in recent minor versions. Its key differentiator is its minimal footprint and pure JavaScript implementation, making it suitable for both Node.js and browser environments (when dealing with byte arrays). It is based on the `image-size` library but aims for a more focused and potentially more modern approach.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates fetching an image, converting its response to a Uint8Array, and using `imageMeta` to extract metadata like type, width, height, and orientation. Includes error handling.

import { imageMeta } from 'image-meta';

async function getImageMetadata(imageUrl: string) {
  try {
    const response = await fetch(imageUrl);
    if (!response.ok) {
      throw new Error(`Failed to fetch image: ${response.statusText}`);
    }
    // For Node.js, you might need 'node-fetch' and res.buffer()
    // For browser, you'd use res.arrayBuffer() and then new Uint8Array()
    const arrayBuffer = await response.arrayBuffer();
    const data = new Uint8Array(arrayBuffer);

    const meta = imageMeta(data);
    console.log(`Image Type: ${meta.type}`);
    console.log(`Dimensions: ${meta.width}x${meta.height}`);
    if (meta.orientation) {
      console.log(`Orientation: ${meta.orientation}`);
    }
    return meta;
  } catch (error) {
    console.error('Error detecting image meta:', error);
    throw error; // Re-throw to indicate failure
  }
}

// Example usage with a placeholder URL
getImageMetadata('https://example.com/some-image.jpg')
  .then(meta => console.log('Successfully retrieved meta:', meta))
  .catch(err => console.error('Overall error:', err));

view raw JSON →