icojs
TypeScript icon, indicating that this package has built-in type declarations

0.19.4 • Public • Published

icojs

npm CI Coverage Status codebeat badge

A JavaScript library to use ICO. Works on both Node.js and the browser.

Install

npm install icojs

Node.js:

import { isICO, parseICO } from 'icojs';

Browser:

import { isICO, parseICO } from 'icojs/browser';

or

<script type="text/javascript" src="node_modules/icojs/dist/ico.js"></script>

To fully use this library, browsers must support JavaScript typed arrays, Canvas API and Promise. Chrome, Edge 12, Firefox and Safari 9 support these functions.

Example

Node.js:

import { readFile, writeFile } from 'node:fs/promises';
import { parseICO } from 'icojs';

const buffer = await readFile('favicon.ico');
const images = await parseICO(buffer, 'image/png');
// save as png files
images.forEach(image => {
  const file = `${image.width}x${image.height}-${image.bpp}bit.png`;
  const data = Buffer.from(image.buffer);
  writeFile(file, data);
});

Browser:

<input type="file" id="input-file" />
<script>
  document.getElementById('input-file').addEventListener('change', evt => {
    // use FileReader for converting File object to ArrayBuffer object
    var reader = new FileReader();
    reader.onload = async e => {
      const images = await ICO.parseICO(e.target.result);
      // logs images
      console.dir(images);
    };
    reader.readAsArrayBuffer(evt.target.files[0]);
  }, false);
</script>

Demo

https://egy186.github.io/icojs/#demo

API

ICO

isICO(source) ⇒ boolean

Check the ArrayBuffer is valid ICO.

Kind: global method of ICO
Returns: boolean - True if arg is ICO.

Param Type Description
source ArrayBuffer | Buffer ICO file data.

parseICO(buffer, [mime]) ⇒ Promise.<Array.<ParsedImage>>

Parse ICO and return some images.

Kind: global method of ICO
Returns: Promise.<Array.<ParsedImage>> - Resolves to an array of ParsedImage.

Param Type Default Description
buffer ArrayBuffer | Buffer ICO file data.
[mime] string "image/png" MIME type for output.

Typedefs

ParsedImage : object

Kind: global typedef
Properties

Name Type Description
width number Image width.
height number Image height.
bpp number Image color depth as bits per pixel.
buffer ArrayBuffer Image buffer.

License

MIT license

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.19.4
654latest

Version History

VersionDownloads (Last 7 Days)Published
0.19.4
654
0.19.3
13
0.19.2
0
0.19.1
0
0.19.00
0.18.04
0.17.1455
0.17.08
0.16.181
0.16.01
0.15.12
0.15.00
0.14.030
0.13.10
0.13.00
0.12.335
0.12.20
0.12.10
0.12.00
0.11.00
0.10.10
0.10.00
0.9.13
0.9.00
0.8.10
0.8.00
0.7.21
0.7.10
0.7.00
0.6.00
0.5.01
0.5.0-alpha0
0.4.16
0.4.00
0.4.0-alpha.10
0.4.0-alpha0
0.3.00
0.3.0-alpha.30
0.3.0-alpha.10
0.3.0-alpha0
0.2.00

Package Sidebar

Install

npm i icojs

Weekly Downloads

1,294

Version

0.19.4

License

MIT

Unpacked Size

55.6 kB

Total Files

15

Last publish

Collaborators

  • egy186