New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bun-image-size

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bun-image-size

A tiny library to process image sizes and types

  • 0.2.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-93.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 23 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc