{"library":"pngquant","title":"PNGQuant Stream Wrapper","description":"The `pngquant` package provides a Node.js readable/writable stream wrapper for the `pngquant` command-line utility. It allows developers to optimize PNG images by reducing their color palette directly within a Node.js stream pipeline, making it suitable for processing images from HTTP requests, file uploads, or other stream sources without requiring temporary files. Currently at version 4.2.0, its release cadence is not explicitly stated in the provided information, but it appears actively maintained given recent version bumps and project activity. Key differentiators include its efficient stream-based API, direct integration with the powerful `pngquant` CLI for high-quality optimization, and robust TypeScript type definitions for enhanced developer experience. This design simplifies image optimization workflows in server-side applications and microservices.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pngquant"],"cli":null},"imports":["import PngQuant from 'pngquant';","const PngQuant = require('pngquant');","import type { PngQuantOptions } from 'pngquant';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import PngQuant from 'pngquant';\nimport { createReadStream, createWriteStream, promises as fsPromises } from 'fs';\nimport { pipeline } from 'stream/promises';\nimport path from 'path';\n\n// Ensure a dummy input.png exists for this example to run.\n// In a real application, 'input.png' would be provided by user upload or file system.\nconst inputFilePath = path.resolve(__dirname, 'input.png');\nconst outputFilePath = path.resolve(__dirname, 'output.png');\n\nasync function createDummyPng() {\n  const dummyPngContent = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64');\n  await fsPromises.writeFile(inputFilePath, dummyPngContent);\n  console.log(`Created dummy PNG at ${inputFilePath}`);\n}\n\nasync function optimizeImage() {\n  await createDummyPng(); // Create a dummy PNG for the example\n  try {\n    // Initialize PngQuant with specific command-line arguments for the pngquant binary.\n    // Here: reduce to 192 colors, target quality 60-80, disable dithering, read from stdin ('-').\n    const myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);\n    console.log(`Optimizing ${inputFilePath} to ${outputFilePath}...`);\n\n    await pipeline(\n      createReadStream(inputFilePath), // Source stream: reads the input PNG\n      myPngQuanter,                  // Transform stream: optimizes the PNG\n      createWriteStream(outputFilePath)  // Destination stream: writes the optimized PNG\n    );\n    console.log(`Optimization complete! Optimized PNG saved to ${outputFilePath}`);\n  } catch (error) {\n    console.error('Image optimization failed:', error);\n  } finally {\n    // Clean up the dummy file\n    await fsPromises.unlink(inputFilePath).catch(() => {});\n  }\n}\n\noptimizeImage();","lang":"typescript","description":"Demonstrates how to optimize a PNG file from a read stream to a write stream using custom `pngquant` command-line options within a Node.js pipeline.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}