@jcayzac/image-information
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

@jcayzac/image-information

Lightweight detection of local or remote images' type, orientation & size in pure Javascript.

license npm version npm downloads bundle

This module detect informations about local or remote images. For remote images, it streams the data only until it finds information.

Images in the following formats are supported: GIF, HEIC, HEIF, AVIF, J2C, JP2, JPEG, PNG, SVG & WebP.

Usage

# pnpm
pnpm add @jcayzac/image-information

# bun
bunx add @jcayzac/image-information

# npm
npx add @jcayzac/image-information

# yarn
yarn add @jcayzac/image-information

# deno
deno add npm:@jcayzac/image-information

Getting information about an image from a URL

import { imageInformation } from '@jcayzac/image-information'

// Remote URL
const info = await imageInformation('https://example.com/image.jpg')
if (info !== undefined) {
  const {
    // intrinsic width.
    width,

    // intrinsic height.
    height,

    // inferred file extension (doesn't care about URL).
    extension,

    // inferred MIME type.
    type,

    // detected EXIF orientation, if any.
    orientation,
  } = info

  console.log(`Found information:`, info)
}

// File URL
const info2 = await imageInformation('file:///tmp/image.jpg')

[!NOTE]

The function accepts strings as a convenience, but those are expected to represent valid URLs. For local images, you must use the file:// URL scheme. Passing paths directly will not work.

Getting information about an image from a buffer

The probe() function can be used to get information about an image from a buffer (either an Uint8Array or a node Buffer).

import { readFile } from 'node:fs/promises'
import { probe } from '@jcayzac/image-information'

const buffer = await readFile('image.jpg')
const info = await probe(buffer)

Like it? Buy me a coffee!

If you like anything here, consider buying me a coffee using one of the following platforms:

GitHub Sponsors Revolut Wise Ko-Fi PayPal


Package Sidebar

Install

npm i @jcayzac/image-information

Weekly Downloads

10

Version

1.1.1

License

MIT

Unpacked Size

17.4 kB

Total Files

7

Last publish

Collaborators

  • jcayzac