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

sharp-ico

v0.1.5

Published

ICO encoder and decoder for sharp base on ico-endec.

Downloads

190,616

Readme

sharp-ico

ICO encoder and decoder for sharp base on ico-endec (for encode) and decode-ico (for decode).

Install

npm install sharp-ico

Usage

Create instances of sharp from an ICO image

ico.sharpsFromIco(input, options?, resolveWithObject?)

  • input (string | Buffer) - A String containing the filesystem path to an ICO image file, or a Buffer containing ICO image data.
  • options Object (optional) - sharp constructor options.
  • resolveWithObject boolean (optional) - Return an array of Object containing image (instance of sharp) property and decoding info instead of only instance of sharp. Default by false.

Returns Sharp[] | ImageData[] - Return an array of instance of sharp or Object containing image (instance of sharp) property and decoding info.

const ico = require("sharp-ico");

ico
  .sharpsFromIco("input.ico", {
    // sharp constructor options
  }) // returns an array of instance of sharp
  .forEach(async (icon, index) => {
    icon.toFile(`output-${index}.png`);

    // Or
    const metadata = await icon.metadata();
    icon.toFile(`output-${metadata.width}x${metadata.height}.png`);
  });

// Set the third option to `true`, will return objects with decoding info
ico
  .sharpsFromIco("input.ico", null, true)
  .forEach((icon) => {
    icon.image.toFile(`output-${icon.width}x${icon.height}.png`);
  });

Write an ICO file

ico.sharpsToIco(icons, fileOut, options?)

  • icons Sharp[] - An array of instance of sharp.
  • fileOut string - The path to write the image data to.
  • options Object (optional)
    • sizes (number[] | "default") - Array of sizes to use when resizing. "default" equal to [256, 128, 64, 48, 32, 24, 16].
    • resizeOptions Object (optional) - sharp resize options.

Returns Promise<Object> - Resolve with an Object containing size, width, height properties.

const sharp = require("sharp");
const ico= require("sharp-ico");
const bmp = require("sharp-bmp"); // if need to write bmp icons

ico
  .sharpsToIco(
    [
      sharp("input-256x256.png"),
      bmp.sharpFromBmp("input-64x64.bmp"),
      sharp("input-32x32.png"),
      // more sizes...
    ],
    "output.ico"
  )
  .then((info) => {
    console.log(info); // { size, width, height }
  })
  .catch((err) => {
    console.error(err);
  });

// sizes options
ico
  .sharpsToIco(
    [
      sharp("input-256x256.png")
    ],
    "output.ico",
    {
      sizes: [64, 32, 24],
      // sizes: "default", // equal to [256, 128, 64, 48, 32, 24, 16]
      resizeOptions: {}, // sharp resize optinos
    }
  ); // will output a 64x64 ico image (with 32x32 and 24x24 sizes)

Decode ICO

ico.decode(buffer)

  • buffer Buffer - A Buffer containing ICO image data.

Returns Object[] - Return an array of Object contains the following decoding info:

  • width number - The width of the image, in pixels.
  • height number - The height of the image, in pixels.
  • type string - The type of image, will be one of bmp or png.
  • data Uint8Array - The data of the image, format depends on type, see below.
  • bpp number - The color depth of the image as the number of bits used per pixel.
  • hotspot null | { x: number, y: number } - If the image is a cursor (.cur), this is the hotspot.

The format of the data parameter depends on the type of image. When the image is of type bmp, the data array will hold raw pixel data in the RGBA order, with integer values between 0 and 255 (included). When the type is png, the array will be png data.

const fs = require("fs");
const sharp = require("sharp");
const ico = require("sharp-ico");

const buffer = fs.readFileSync("input.ico");
const icons = ico.decode(buffer);

icons.forEach((icon) => {
  const image = icon.type === "png"
    ? sharp(icon.data)
    : sharp(icon.data, {
        raw: {
          width: icon.width,
          height: icon.height,
          channels: 4,
        },
      });
  image.toFile(`output-${icon.width}x${icon.height}.png`);
});

Encode ICO

ico.encode(bufferList)

  • bufferList Buffer[] - An array of Buffer containing PNG or BMP image data.

Returns Buffer - Return a buffer containing ICO image data.

const fs = require("fs");
const sharp = require("sharp");
const ico = require("sharp-ico");
const bmp = require("sharp-bmp"); // if need to write bmp icons

(async () => {
  const icons = [
    sharp("input-256x256.png"),
    bmp.sharpFromBmp("input-64x64.bmp"),
    sharp("input-32x32.png"),
  ];
  const bufferList = [];
  for (let i = 0; i < icons.length; i++) {
    const buffer = await icons[i].toFormat("png").toBuffer();
    bufferList.push(buffer);
  }
  const icoBuffer = ico.encode(bufferList);
  fs.writeFileSync("output.ico", icoBuffer);

  console.log(icoBuffer.length); // size of output.ico
})();

Change Log

0.1.1

  • sharpsToIco support sizes option

0.1.5