Coder Social home page Coder Social logo

icojs's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.