0.2.2 • Published 2 years ago

bun-image-size v0.2.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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 + typeTest DurationNumber of iterationsIterations per second
NodeJS + JPG10,000ms251,83825,184
Bun + JPG10,000ms494,25749,426 (1.96x faster)
NodeJS + PNG10,000ms237,05723,706
Bun + PNG10,000ms1,212,458121,246 (5.11x faster)
NodeJS + SVG10,000ms193,88319,388
Bun + SVG10,000ms663,20466,320 (3.42x faster)
NodeJS + ICO10,000ms252,12825,213
Bun + ICO10,000ms1,037,482103,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

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago