### Install png2icons with npm Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Install the png2icons module using npm. This is the first step before using its CLI or API. ```bash npm install png2icons ``` -------------------------------- ### png2icons Example Usage Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md An example demonstrating how to convert a PNG to both ICNS and PNG-based ICO formats, with verbose output enabled. The output file extension is automatically determined by the format. ```bash png2icons sample.png icon -allp -bc -i ``` -------------------------------- ### Complete Icon Generation Workflow Source: https://context7.com/idesis-gmbh/png2icons/llms.txt A comprehensive example demonstrating how to generate ICNS and multiple ICO formats from a single PNG source, including error handling for file reading, directory creation, logging, and cache cleanup. ```javascript const png2icons = require("png2icons"); const fs = require("fs"); const path = require("path"); function generateIcons(inputPath, outputDir, baseName) { // Enable logging png2icons.setLogger(console.log); // Read source PNG (ideally 1024x1024 RGBA) let input; try { input = fs.readFileSync(inputPath); } catch (err) { console.error("Failed to read input file:", err.message); return false; } // Ensure output directory exists if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } const results = { icns: false, icoBmp: false, icoPng: false, icoExe: false }; // Generate Apple ICNS (for macOS apps) const icns = png2icons.createICNS(input, png2icons.BICUBIC, 0); if (icns) { fs.writeFileSync(path.join(outputDir, `${baseName}.icns`), icns); results.icns = true; } // Generate ICO with BMP (for legacy Windows support) const icoBmp = png2icons.createICO(input, png2icons.BICUBIC, 0, false); if (icoBmp) { fs.writeFileSync(path.join(outputDir, `${baseName}-bmp.ico`), icoBmp); results.icoBmp = true; } // Generate ICO with PNG (smaller file size) const icoPng = png2icons.createICO(input, png2icons.BICUBIC, 128, true); if (icoPng) { fs.writeFileSync(path.join(outputDir, `${baseName}-png.ico`), icoPng); results.icoPng = true; } // Generate ICO for Windows executables (Electron, etc.) const icoExe = png2icons.createICO(input, png2icons.BICUBIC, 0, false, true); if (icoExe) { fs.writeFileSync(path.join(outputDir, `${baseName}.ico`), icoExe); results.icoExe = true; } // Clean up cache png2icons.clearCache(); png2icons.setLogger(null); console.log("\nGeneration complete:", results); return results; } // Usage generateIcons("./source/app-logo.png", "./build/icons", "app-icon"); ``` -------------------------------- ### Programmatic API: Create ICNS with default settings Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate an Apple ICNS file from a PNG buffer using the createICNS function. This example uses bicubic interpolation and no color reduction, writing the output to a file. ```javascript const png2icons = require("png2icons"); const fs = require("fs"); // Read the source PNG file const input = fs.readFileSync("app-logo.png"); // Create ICNS with bicubic interpolation, lossless (0 colors = no reduction) const icnsBuffer = png2icons.createICNS(input, png2icons.BICUBIC, 0); if (icnsBuffer) { fs.writeFileSync("app-icon.icns", icnsBuffer); console.log("ICNS file created successfully"); } else { console.error("Failed to create ICNS file"); } ``` -------------------------------- ### CLI: Create both ICNS and ICO files Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Convert a PNG to both Apple ICNS and Microsoft ICO formats simultaneously using the -all flag. ```bash # Create both ICNS and ICO files png2icons logo.png myicon -all ``` -------------------------------- ### CLI: Create Microsoft ICO file (PNG format) Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Create a Microsoft ICO file using PNG format for smaller file sizes, suitable for Windows Vista and later. Use the -icop flag. ```bash # Create Microsoft ICO with PNG format (smaller file size) png2icons logo.png myicon -icop ``` -------------------------------- ### CLI: Create Microsoft ICO file (BMP format) Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate a Microsoft ICO file using BMP format for best compatibility with older Windows versions. Use the -ico flag. ```bash # Create Microsoft ICO with BMP format (best compatibility) png2icons logo.png myicon -ico ``` -------------------------------- ### CLI: Use Bezier interpolation with info output Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate ICNS and ICO files using Bezier interpolation for high-quality scaling and enable verbose output with the -i flag. Use -allwe for combined formats and Windows executable optimization. ```bash # Use Bezier interpolation with info output png2icons logo.png myicon -allwe -bz -i ``` -------------------------------- ### CLI: Create ICO optimized for Windows executables Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate an ICO file optimized for embedding in Windows executables, using a mixed format for compatibility and size. Use the -icowe flag. ```bash # Create ICO optimized for Windows executables png2icons logo.png myicon -icowe ``` -------------------------------- ### createICO(input, scalingAlgorithm, numOfColors, usePNG, forWinExe) Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generates a Microsoft ICO file from a PNG buffer, with options for BMP/PNG formats and Windows executable optimization. ```APIDOC ## createICO(input, scalingAlgorithm, numOfColors, usePNG, forWinExe) ### Description Creates a Microsoft ICO file containing icons at sizes 16, 24, 32, 48, 64, 72, 96, 128, and 256 pixels. ### Parameters - **input** (Buffer) - Required - The source PNG image data. - **scalingAlgorithm** (Number) - Required - The constant representing the interpolation algorithm. - **numOfColors** (Number) - Required - Maximum number of colors for reduction. - **usePNG** (Boolean) - Required - If true, uses PNG format for icons; if false, uses BMP. - **forWinExe** (Boolean) - Optional - If true, optimizes the ICO for Windows executables (mixed BMP/PNG). ### Response - **Buffer** - The generated ICO file data or null if creation fails. ``` -------------------------------- ### Generate icons with png2icons Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Demonstrates reading a PNG file and generating ICNS and ICO files using different scaling algorithms and configuration options. ```javascript var png2icons = require("png2icons"); var fs = require("fs"); var input = fs.readFileSync("sample.png"); // Apple ICNS with bilinear interpolation and no color reduction. // Log infos via console.log. png2icons.setLogger(console.log); var output = png2icons.createICNS(input, png2icons.BILINEAR, 0); if (output) { fs.writeFileSync("icon.icns", output); } // Microsoft ICO using PNG icons with Bezier interpolation and // reduction to 20 colors. // Log infos via console.log (logging function already set before). output = png2icons.createICO(input, png2icons.BEZIER, 20, true); fs.writeFileSync("icon_png.ico", output); // Microsoft ICO using BMP icons with Hermite interpolation, // (numOfColors is ignored). Turn off any logging again. png2icons.setLogger(null); output = png2icons.createICO(input, png2icons.HERMITE, 0, false); fs.writeFileSync("icon_bmp.ico", output); // Microsoft ICO using PNG and BMP icons with alternative bicubic // interpolation, (numOfColors applies!). Suitable for embedding // the icon file in Windows executables. Logging is already off. output = png2icons.createICO(input, png2icons.BICUBIC2, 0, false, true); fs.writeFileSync("icon_winexe.ico", output); ``` -------------------------------- ### Programmatic API: Create ICO with PNG format Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Create a Microsoft ICO file with icons stored in PNG format for smaller file sizes, suitable for Windows Vista and newer. This uses the createICO function with `usePNG` set to true. ```javascript // Create ICO with PNG icons (smaller file size, Windows Vista+) const icoPng = png2icons.createICO(input, png2icons.BEZIER, 128, true); if (icoPng) { fs.writeFileSync("app-icon-png.ico", icoPng); } ``` -------------------------------- ### CLI: Use Hermite interpolation Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Create an Apple ICNS file using Hermite interpolation for high-quality scaling. The -i flag provides info output. ```bash # Use high-quality Hermite interpolation png2icons logo.png myicon -icns -hm -i ``` -------------------------------- ### png2icons Command Line Syntax Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Basic syntax for using the png2icons command-line tool. Specify input, output, format, and optional interpolation and info flags. ```bash png2icons infile outfile format [-nn | - bl | -bc | -bz | -hm | -bc2] [-i] ``` -------------------------------- ### Programmatic API: Create ICO optimized for Windows executables Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate a Microsoft ICO file optimized for embedding in Windows executables. This uses BMP for smaller icon sizes and PNG for larger ones, controlled by the `forWinExe` parameter. ```javascript // Create ICO optimized for Windows executables (Electron apps, etc.) // Uses BMP for sizes < 64px, PNG for larger sizes const icoExe = png2icons.createICO(input, png2icons.BICUBIC2, 0, false, true); if (icoExe) { fs.writeFileSync("app-icon-exe.ico", icoExe); console.log("ICO for Windows executable created"); } ``` -------------------------------- ### createICO - Microsoft ICO Format Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Generates a Microsoft ICO file from a PNG input buffer. Supports PNG or BMP formats for icons within the ICO, and options for Windows executable embedding. ```APIDOC ## POST /api/createICO ### Description Creates a Microsoft ICO file from a PNG input buffer. Allows specifying whether to use PNG or BMP for icons within the ICO, and an option for Windows executable compatibility. ### Method POST ### Endpoint /api/createICO ### Parameters #### Request Body - **input** (Buffer) - Required - Buffer containing the raw content of a PNG file. - **scalingAlgorithm** (Number) - Optional - Algorithm for scaling images. Defaults to a suitable value. Can be one of: - `RESIZE_NEAREST_NEIGHBOR = 0` - `RESIZE_BILINEAR = 1` - `RESIZE_BICUBIC = 2` - `RESIZE_BEZIER = 3` - `RESIZE_HERMITE = 4` - `RESIZE_BICUBIC2 = 5` - **numOfColors** (Number) - Optional - Controls color reduction. `0` for lossless, `>0` for reduction. Ignored if `usePNG` is `false` and `forWinExe` is `false`. Defaults to `0`. - **usePNG** (Boolean) - Optional - If `true`, uses PNG for icons. If `false`, uses Windows bitmaps. Defaults to `true`. - **forWinExe** (Boolean) - Optional - If `true`, creates a mix of PNG and BMP icons (16, 24, 32, 48 in BMP, others in PNG). Recommended for embedding in Windows executables. Defaults to `false`. ### Request Example ```json { "input": "", "scalingAlgorithm": 3, "numOfColors": 20, "usePNG": true, "forWinExe": false } ``` ### Response #### Success Response (200) - **output** (Buffer) - Buffer containing the binary data of the generated ICO file. #### Response Example ```json { "output": "" } ``` #### Error Response (500) - **error** (String) - Description of the error. #### Error Example ```json { "error": "Failed to create ICO file." } ``` ``` -------------------------------- ### Programmatic API: Create ICO with BMP format Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generate a Microsoft ICO file using BMP format for icons, ensuring maximum compatibility with older Windows systems. This uses the createICO function with `usePNG` set to false. ```javascript const png2icons = require("png2icons"); const fs = require("fs"); const input = fs.readFileSync("app-logo.png"); // Create ICO with BMP icons (maximum compatibility with older Windows) const icoBmp = png2icons.createICO(input, png2icons.BICUBIC, 0, false); if (icoBmp) { fs.writeFileSync("app-icon-bmp.ico", icoBmp); } ``` -------------------------------- ### createICNS(input, scalingAlgorithm, numOfColors) Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Generates an Apple ICNS file from a PNG buffer, supporting multiple resolutions and optional color reduction. ```APIDOC ## createICNS(input, scalingAlgorithm, numOfColors) ### Description Creates an Apple ICNS file containing icons at multiple resolutions (16x16 through 512x512@2). The function generates all standard icon sizes including retina variants. ### Parameters - **input** (Buffer) - Required - The source PNG image data. - **scalingAlgorithm** (Number) - Required - The constant representing the interpolation algorithm (e.g., png2icons.BICUBIC). - **numOfColors** (Number) - Required - Maximum number of colors for reduction (0 for no reduction). ### Response - **Buffer** - The generated ICNS file data or null if creation fails. ``` -------------------------------- ### CLI: Create Apple ICNS file Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Use the command-line interface to convert a PNG file to an Apple ICNS format. Specify the input PNG, output base name, and the -icns flag. ```bash # Create Apple ICNS file png2icons logo.png myicon -icns ``` -------------------------------- ### Configure Logging with setLogger Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Set a custom logging function to capture info and error messages. The logger must match the console.log signature. Pass null to disable logging. ```javascript const png2icons = require("png2icons"); const fs = require("fs"); // Enable console logging png2icons.setLogger(console.log); // Custom logger with timestamps png2icons.setLogger((message, ...params) => { console.log(`[${new Date().toISOString()}]`, message, ...params); }); const input = fs.readFileSync("logo.png"); const output = png2icons.createICNS(input, png2icons.BILINEAR, 0); // Logs: png2icons wrote type ic12 for size 32x32@2 with 64 pixels // Logs: png2icons wrote type ic07 for size 128x128 with 128 pixels // ... (continues for each icon size) // Logs: png2icons done // Disable logging png2icons.setLogger(null); ``` -------------------------------- ### Programmatic API: Create compressed ICNS Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Create a compressed Apple ICNS file by specifying a maximum number of colors (e.g., 256) and using Hermite interpolation. This can reduce file size. ```javascript // Create ICNS with color reduction for smaller file size (256 colors max) const compressedIcns = png2icons.createICNS(input, png2icons.HERMITE, 256); if (compressedIcns) { fs.writeFileSync("app-icon-compressed.icns", compressedIcns); } ``` -------------------------------- ### Scaling Algorithm Constants Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Constants available in the png2icons module to specify image interpolation algorithms during resizing. Each constant corresponds to a different quality and performance trade-off. ```javascript const png2icons = require("png2icons"); // Available scaling algorithms: png2icons.NEAREST_NEIGHBOR // 0 - Fastest, mediocre quality png2icons.BILINEAR // 1 - Fast, OK quality png2icons.BICUBIC // 2 - Slower, good to very good quality (default) png2icons.BEZIER // 3 - Quite slow, high quality png2icons.HERMITE // 4 - Quite slow, high quality png2icons.BICUBIC2 // 5 - Fast alternative bicubic, good quality ``` -------------------------------- ### Define png2icons API functions Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md The module exports four primary functions for icon generation, logging, and cache management. ```javascript function createICNS(input, scalingAlgorithm, numOfColors) function createICO(input, scalingAlgorithm, numOfColors, usePNG, forWinExe) function setLogger(logFn) function clearCache() ``` -------------------------------- ### setLogger - Custom Logging Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Allows setting a custom logging function to capture output from the png2icons module. ```APIDOC ## POST /api/setLogger ### Description Sets a custom logging function. The provided function will receive log messages from the png2icons module. ### Method POST ### Endpoint /api/setLogger ### Parameters #### Request Body - **logFn** (Function | null) - Optional - The logging function to use. Should accept arguments similar to `console.log`. If `null`, logging is disabled. ### Request Example ```json { "logFn": "console.log" } ``` ### Response #### Success Response (200) - **message** (String) - Confirmation message. #### Response Example ```json { "message": "Logger set successfully." } ``` ``` -------------------------------- ### createICNS - Apple ICNS Format Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Generates an Apple ICNS file from a PNG input buffer. Supports various scaling algorithms and color reduction. ```APIDOC ## POST /api/createICNS ### Description Creates an Apple ICNS file from a PNG input buffer. ### Method POST ### Endpoint /api/createICNS ### Parameters #### Request Body - **input** (Buffer) - Required - Buffer containing the raw content of a PNG file. - **scalingAlgorithm** (Number) - Optional - Algorithm for scaling images. Defaults to a suitable value. Can be one of: - `RESIZE_NEAREST_NEIGHBOR = 0` - `RESIZE_BILINEAR = 1` - `RESIZE_BICUBIC = 2` - `RESIZE_BEZIER = 3` - `RESIZE_HERMITE = 4` - `RESIZE_BICUBIC2 = 5` - **numOfColors** (Number) - Optional - Controls color reduction. `0` for lossless, `>0` for reduction. Defaults to `0`. ### Request Example ```json { "input": "", "scalingAlgorithm": 1, "numOfColors": 0 } ``` ### Response #### Success Response (200) - **output** (Buffer) - Buffer containing the binary data of the generated ICNS file. #### Response Example ```json { "output": "" } ``` #### Error Response (500) - **error** (String) - Description of the error. #### Error Example ```json { "error": "Failed to create ICNS file." } ``` ``` -------------------------------- ### Define scaling algorithm constants Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Constants used to specify the interpolation algorithm for image scaling. ```javascript RESIZE_NEAREST_NEIGHBOR = 0; RESIZE_BILINEAR = 1; RESIZE_BICUBIC = 2; RESIZE_BEZIER = 3; RESIZE_HERMITE = 4; RESIZE_BICUBIC2 = 5; ``` -------------------------------- ### Clear Internal Image Cache with clearCache Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Frees memory by clearing the internal cache of scaled images. Call this after processing is complete, especially when dealing with large input files or multiple icon format generations. ```javascript const png2icons = require("png2icons"); const fs = require("fs"); const input = fs.readFileSync("large-logo.png"); // Create multiple formats - caching speeds up subsequent calls const icns = png2icons.createICNS(input, png2icons.BICUBIC, 0); const icoBmp = png2icons.createICO(input, png2icons.BICUBIC, 0, false); const icoPng = png2icons.createICO(input, png2icons.BICUBIC, 0, true); if (icns) fs.writeFileSync("icon.icns", icns); if (icoBmp) fs.writeFileSync("icon-bmp.ico", icoBmp); if (icoPng) fs.writeFileSync("icon-png.ico", icoPng); // Free cached image data when done png2icons.clearCache(); console.log("Cache cleared, memory freed"); ``` -------------------------------- ### setLogger(logger) Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Sets an external logging function to receive info and error messages during processing. The logger function must be compatible with console.log signature. By default, no logging occurs. ```APIDOC ## setLogger(logger) ### Description Sets an external logging function to receive info and error messages during processing. The logger function must be compatible with console.log signature. By default, no logging occurs. ### Method `png2icons.setLogger(loggerFunction | null)` ### Parameters #### Arguments - **logger** (function | null) - Required - A function compatible with `console.log` signature to receive log messages, or `null` to disable logging. ### Request Example ```javascript const png2icons = require("png2icons"); // Enable console logging png2icons.setLogger(console.log); // Custom logger with timestamps png2icons.setLogger((message, ...params) => { console.log(`[${new Date().toISOString()}]`, message, ...params); }); // ... processing code ... // Disable logging png2icons.setLogger(null); ``` ### Response This method does not return a value. It configures the logger for subsequent operations. ``` -------------------------------- ### clearCache - Cache Management Source: https://github.com/idesis-gmbh/png2icons/blob/master/README.md Frees all internally cached image data, which can be useful for memory management. ```APIDOC ## POST /api/clearCache ### Description Clears the internal cache used by png2icons to store processed image data, potentially freeing up memory. ### Method POST ### Endpoint /api/clearCache ### Parameters None ### Request Example ```json {} ``` ### Response #### Success Response (200) - **message** (String) - Confirmation message. #### Response Example ```json { "message": "Cache cleared successfully." } ``` ``` -------------------------------- ### clearCache() Source: https://context7.com/idesis-gmbh/png2icons/llms.txt Clears the internal image cache used for storing scaled images. png2icons caches scaled image data to speed up processing when the same input is used to create multiple output formats. Call this function to free memory after processing is complete. ```APIDOC ## clearCache() ### Description Clears the internal image cache used for storing scaled images. This is useful for freeing up memory after multiple icon generation operations using the same input image. ### Method `png2icons.clearCache()` ### Parameters This method does not accept any parameters. ### Request Example ```javascript const png2icons = require("png2icons"); // ... perform icon generation ... // Free cached image data when done png2icons.clearCache(); console.log("Cache cleared, memory freed"); ``` ### Response This method does not return a value. It clears the internal cache. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.