Node.js wrapper for some images compression algorithms
npm install imageoptimNode.js wrapper for image compression algorithms.
- Types
- Patches
- Overview
- Install
- Usage
- API
- imageOptim.optim
- imageOptim.lint
- Exit code
- SUCCESS
- CANT_COMPRESS
- DOESNT_EXIST
- Example
- CLI
- Example
* PNG – PNGOUT, Zopflipng, Pngcrush, AdvPng and OptiPNG.
Supporting of other types of images is coming soon.
You can view all the _patches_ which are applied to the algorithms here.
#### Overview:
* optipng.patch – adds to OptiPNG the ability to remove _RGB_ components or transparent pixels in _RGB+alpha_ images.
* zopflipng.patch – makes Zopflipng work on _Linux_.
``bash`
$ npm install imageoptim
This command will install image-optim and all supported compression algorithms automatically. The installation of the compression algorithms is subscribed in script env-setup.
`js`
var imageOptim = require('imageoptim');
#### imageOptim.optim
Optimizes the given files.
@param {String[]} – a list of paths to files to optimize
@param {Object} – options:
reporters {String[]}* - reporters of the results. _flat_ - writes the results to stdout, _html_ - creates the HTML report of the results in file imageoptim-report.html (default: flat).
@returns {Promise Object[]}* – the information about optimized files:
`js`
[{ name: 'file.ext', savedBytes: 12345, exitCode: 0 }]
#### imageOptim.lint
Checks whether the given files can be optimized further.
@param {String[]} – a list of paths to files to check
@param {Object} – options:
tolerance {Number}* – sets the measurement error in _percentages_ (decimal < 1) or _bytes_ (integer or decimal >= 1) (default: 0).
type | skope | description
--- | --- | ---
_percentages_ | < 1 | The file will be considered to be optimized if the percentage of saved bytes after the compression is less than the specified value (_0.8_ – 80%, _0.01_ – 1%, etc)20 bytes
_bytes_ | >= 1 | The file will be considered to be optimized if the number of saved bytes after the compression is less than the specified value (_20_ – , _100500_ – 100500 bytes, etc)
reporters {String[]}* - reporters of the results. _flat_ - writes the results to stdout, _html_ - creates the HTML report of the results in file imageoptim-report.html (default: flat).
@returns {Promise Object[]}* – the information about linted files:
`js`
[{ name: 'file.ext', isOptimized: true, exitCode: 0 }]
#### Exit code
##### imageOptim.SUCCESS
If a file was processed without errors its exit code will be equal to 0.
##### imageOptim.CANT_COMPRESS
If a file can not be processed by one of the algorithms its exit code will be equal to 1.
##### imageOptim.DOESNT_EXIST
If a file does not exist its exit code will be equal to 2.
#### Example
`js
var imageOptim = require('imageoptim');
// optimization
imageOptim.optim(['1.png', '2.png'], { reporters: ['flat', 'html'] })
.then(function (res) {
console.log(res);
})
.done();
// linting
imageOptim.lint(['1.png', '2.png'], {
tolerance: 0.08,
// tolerance: 20,
reporters: ['flat', 'html']
})
.then(function (res) {
console.log(res);
})
.done();
`
`bash
$ imageoptim --help
Node.js wrapper for image compression algorithms
Usage:
imageoptim [OPTIONS] [ARGS]
Options:
-h, --help : Help
-v, --version : Shows the version number
-l, --lint : Lint mode
-t TOLERANCE, --tolerance=TOLERANCE : sets the measurement error in percentages or bytes (default: 0)
-r REPORTERS, --reporter=REPORTERS : flat or/and html (default: flat)
Arguments:
FILES : Paths to files (required)
`
REMARK! More information about options lint and tolerance can be found in the API.
#### Example
`bash
$ imageoptim path/to/file1 path/to/file2 --reporter=flat --reporter=html # optimization
$ imageoptim path/to/file --lint --tolerance=0.08 --reporter=flat --reporter=html # linting, tolerance is 8%
$ imageoptim path/to/file --lint --tolerance=20 # linting, tolerance is 20 bytes``