PNGQuant Stream Wrapper

4.2.0 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import PngQuant from 'pngquant';
import { createReadStream, createWriteStream, promises as fsPromises } from 'fs';
import { pipeline } from 'stream/promises';
import path from 'path';

// Ensure a dummy input.png exists for this example to run.
// In a real application, 'input.png' would be provided by user upload or file system.
const inputFilePath = path.resolve(__dirname, 'input.png');
const outputFilePath = path.resolve(__dirname, 'output.png');

async function createDummyPng() {
  const dummyPngContent = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64');
  await fsPromises.writeFile(inputFilePath, dummyPngContent);
  console.log(`Created dummy PNG at ${inputFilePath}`);
}

async function optimizeImage() {
  await createDummyPng(); // Create a dummy PNG for the example
  try {
    // Initialize PngQuant with specific command-line arguments for the pngquant binary.
    // Here: reduce to 192 colors, target quality 60-80, disable dithering, read from stdin ('-').
    const myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);
    console.log(`Optimizing ${inputFilePath} to ${outputFilePath}...`);

    await pipeline(
      createReadStream(inputFilePath), // Source stream: reads the input PNG
      myPngQuanter,                  // Transform stream: optimizes the PNG
      createWriteStream(outputFilePath)  // Destination stream: writes the optimized PNG
    );
    console.log(`Optimization complete! Optimized PNG saved to ${outputFilePath}`);
  } catch (error) {
    console.error('Image optimization failed:', error);
  } finally {
    // Clean up the dummy file
    await fsPromises.unlink(inputFilePath).catch(() => {});
  }
}

optimizeImage();

view raw JSON →