npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bun-image-size

v0.2.2

Published

A tiny library to process image sizes and types

Downloads

5

Readme

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