0.0.2 • Published 5 months ago

@khoohaoyit/image-size v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@khoohaoyit/image-size

A stream based image size extractor in Node

Features:

  • Support variaty of formats
  • Low memory footprint
  • API compatible with image-size
  • Fine control over when to stop parsing
  • No dependancy

Installation

npm install @khoohaoyit/image-size

Usage

Processing buffer

import { SizeExtractor } from "@khoohaoyit/image-size";
import { finished } from 'stream/promises';

const extractor = new SizeExtractor({ passthrough: false });
await finished(
  extractor
    .end(buffer)
);
console.log(extractor.imageSize); // Compatable with `image-size`

Extract image size while writing to a file

import { SizeExtractor } from "@khoohaoyit/image-size";
import { pipeline } from 'stream/promises';
import { createWriteStream } from 'fs';

const extractor = new SizeExtractor({ passthrough: true });
await pipeline(
  await fetch('https://github.githubassets.com/assets/gc_banner_dark-b73fa80c5473.png')
    .then(res => res.body),
  extractor,
  createWriteStream('imageA.png'),
);
console.log(extractor.sizes);

Stop processing once it has done parsing all formats

import { SizeExtractor } from "@khoohaoyit/image-size";
import { pipeline } from 'stream/promises';
import { createReadStream } from 'fs';

const extractor = new SizeExtractor({ passthrough: false });
const controller = new AbortController;
await pipeline(
  createReadStream('imageA.png'),
  extractor
    .once('parseAllDone', () => controller.abort()),
  { signal: controller.signal },
).catch(err => {
  if (controller.signal.aborted)
    return;
  throw err;
});
console.log(extractor.sizes);

Library Comparison

@khoohaoyit/image-sizeprobe-image-sizeimage-size
Stream basedYesYesNo
Support syncNoYesYes
ddsYesNoYes
icnsYesNoYes
j2cYesNoYes
jp2YesNoYes
ktxYesNoYes
pnmYesNoYes
tgaYesNoYes
avifYesYesNo
heicYesYesNo
heifYesYesNo

The listed library on top all supports ico, cur, bmp, gif, jpg, png, psd, svg, tiff, and webp

Documentation

new SizeExtractor(options)

  • options.passthrough: boolean
    • Passes the input to output if true
  • options.whitelistFormats?: string[]
    • Whitelist specific formats
  • options.blacklistFormats?: string[]
    • Blacklist specific formats, ignored if whitelistFormats is set

SizeExtractor.imageSize

The API compatible of image-size result

SizeExtractor.sizes

The extracted sizes emitted from 'size'

Event: 'size'

  • data: { width: number, height: number }
  • format: string

Emitted when a format has successfully extracted the image size

Event: 'parseError'

  • error: unknown
  • format: string

Emitted when a format encountered unexpected error while parsing

Event: 'parseDone'

  • format: string

Emitted when a format has ended parsing

Event: 'parseAllDone'

Emitted when all formats has ended parsing