PNG optimizer, similar to OptiPNG
jspngopt is a tool which tries to reduce the file size of a PNG file by
- removing the alpha channel of an opaque image
- converting a grayscale-only image to a grayscale color mode
- trying out different pixel prediction filters
- trying out different zlib compression parameter combinations
On node.js, simply do
npm install jspngopt
A version which supports browserify is planned. If you need things packaged for some other environment, please file a ticket explaining your setup.
The command line interface of
jspngopt is intended to be
mostly compatible with the one from
But as this project is in the early stages of development,
currently the only thing accepted on the command line is a list of file names,
which will be optimized with the default settings,
the results overwriting the input files if there was a size reduction.
The central element of the API is the
var jspngopt = require("jspngopt"); var opt = new jspngopt.Optimizer(); var optimized = opt.bufferSync(unoptimized);
Optimizer constructor takes an
It may contain the following elements:
- matrices: An array of objects, each of which describes a collection of
possible compression parameters. The keys of each such objects are the same
described below for parameters, while the values are arrays listing all the
values which should be tried for a given parameter. The default corresponds
to the default of
- pako: The
pakomodule, as returned from
require("pako"). Setting this option will use
zlib, ensuring reproducible results but possibly sacrificing performance and compression rate.
- deflateSync: A function which is functionally equivalent
zlib.deflateSyncand which should be used in its stead.
- maxIdatLength: Maximal size of an
IDATchunk, defaults to
0x7fffffff. This is the length of the raw data payload, excluding header and checksum.
- log: Function to call when logging output (at
verbosity> 0). Defaults to
- verbosity: Amount of information printed during processing. The default of 0 avoids all logging. With a setting of 1, the best parameter combination will get printed. At a setting of 2, all the parameter combinations will get printed.
Each parameter combination is described by the following numbers:
- filters: Which filters to try. Numbers 0 through 4 correspond to the
filters None through Paeth described in the PNG specification.
Filter 5 corresponds to an adaptive choice of filters which minimizes
the sum of the absolute values in each row.
- interlace: 0 disables interlacing, while 1 will eventually enable
Adam7 interlacing. Right now interlacing is not supported yet.
- windowBits: Base-two logarithm of the size of the sliding window
used to find redundant data.
Valid values are in the range from 8 through 15.
- level: Compression level, in the range from 0 (no compression)
to 9 (best compression).
- memLevel: Memory usage level, from 1 (little memory) to 9 (large memory).
strategy: Compression strategy.
- 0 is the
DEFAULT_STRATEGYused for generic data.
- 1 is
FILTERED, optimized for the fact that numbers are likely small.
- 2 is
HUFFMANN_ONLYto not match duplicates at all.
- 3 is
RLE, performing only run-length encoding.
- 4 is
FIXEDuses a fixed Huffmann coding table.
[0, 1, 2, 3].
- 0 is the
Methods of Optimizer instances
bufferSync(Buffer) → Buffer
Takes the content of a complete PNG file as input, and returns an optimized version on output. Will throw an exception in case of an error.
fileSync(String) → void
Takes the name of a PNG file which will be read, optimized and written to the same location. Will throw an exception in case of an error. This method is not available in the browserified version.