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", {
})
.forEach(async (icon, index) => {
icon.toFile(`output-${index}.png`);
const metadata = await icon.metadata();
icon.toFile(`output-${metadata.width}x${metadata.height}.png`);
});
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");
ico
.sharpsToIco(
[
sharp("input-256x256.png"),
bmp.sharpFromBmp("input-64x64.bmp"),
sharp("input-32x32.png"),
],
"output.ico"
)
.then((info) => {
console.log(info);
})
.catch((err) => {
console.error(err);
});
ico
.sharpsToIco(
[
sharp("input-256x256.png")
],
"output.ico",
{
sizes: [64, 32, 24],
resizeOptions: {},
}
);
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");
(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);
})();
Change Log
0.1.1
sharpsToIco
support sizes
option
0.1.5