bun-image-size
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Identify image Mime types and dimensions in Bun

The processor can handle absolute file paths or URLs to remote images and ofcourse you can pass the image's raw data as an ArrayBuffer too.

Supported Mime types

Currently the package can only identify and measure the following image types:

  • JPEG
  • PNG
  • SVG
  • ICO
  • WEBP
  • GIF
  • BMP

Getting started

Get image type and dimensions from a remote URLs

import {createProcessor} from 'bun-image-size';

const processor = createProcessor('https://picsum.photos/500/400')
  .onError((error) => console.log(error));

const result = await processor.process();

/** result
 * {
 *   type: "image/png", // Mime type
 *   size: 123009, // The byte length
 *   width: 500, // Pixels
 *   height: 400 // Pixels
 * }
 */

The example above registers the processor with the URL supplied immediately. The URL is optional on initialization. If ommitted and process is called without a location too, an error will be reported.

Get image type and dimensions from a local file

import {createProcessor} from 'bun-image-size';

const processor = createProcessor(import.meta.dir + '/image.png')
  .onError((error) => console.log(error));

const result = await processor.process();

/** result
 * {
 *   type: "image/png", // Mime type
 *   size: 123009, // The byte length
 *   width: 500, // Pixels
 *   height: 400 // Pixels
 * }
 */

Get image type and dimensions from an ArrayBuffer

import {createProcessor} from 'bun-image-size';

let imageBuffer; /* get the buffer from somewhere */;

const processor = createProcessor()
  .onError((error) => console.log(error));

const result = await processor.processArrayBuffer(imageBuffer);

/** result
 * {
 *   type: "image/png", // Mime type
 *   size: 123009, // The byte length
 *   width: 500, // Pixels
 *   height: 400 // Pixels
 * }
 */

Only need the mimetype and not size?

You can use the helper to identify an image's Mime type in Bun.

import {createMimeIdentifier} from 'bun-image-size';

let imageBuffer: ArrayBuffer; // Get the image buffer from somewhere.

const identifier = createMimeIdentifier(imageBuffer);
const mimeType: string = await identifier.identify(); // image/png

Get a copy of the buffer currently being processed

import {createProcessor} from 'bun-image-size';

const processor = createProcessor(import.meta.dir + '/image.png');
const buffer = processor.buffer // Undefined
await processor.process();
const bufferNow = processor.buffer // ArrayBuffer

Benchmarks vs. Node

All tests conducted on:

  • Bun 0.1.10
  • Node 16.14.2
  • bun-image-size 0.1.4
  • image-size 1.0.2
  • 1.6 GHz Dual-Core Intel Core i5 CPU
  • 4GB 1600MHz DDR3 RAM
Runtime + type Test Duration Number of iterations Iterations per second
NodeJS + JPG 10,000ms 251,838 25,184
Bun + JPG 10,000ms 494,257 49,426 (1.96x faster)
NodeJS + PNG 10,000ms 237,057 23,706
Bun + PNG 10,000ms 1,212,458 121,246 (5.11x faster)
NodeJS + SVG 10,000ms 193,883 19,388
Bun + SVG 10,000ms 663,204 66,320 (3.42x faster)
NodeJS + ICO 10,000ms 252,128 25,213
Bun + ICO 10,000ms 1,037,482 103,748 (4.11x faster)

All tests were validated and 0% of the tests gave invalid results. The image sizes varied in the different formats, but they were exactly the same across Runtime tests. The NodeJS version uses image-size

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.2
    1
    • latest

Version History

Package Sidebar

Install

npm i bun-image-size

Weekly Downloads

8

Version

0.2.2

License

MIT

Unpacked Size

25.3 kB

Total Files

5

Last publish

Collaborators

  • eckidevs