UNPKG

2.47 kBMarkdownView Raw
1# imagemin [![Build Status](https://travis-ci.org/imagemin/imagemin.svg?branch=master)](https://travis-ci.org/imagemin/imagemin)
2
3> Minify images seamlessly
4
5---
6
7<div align="center">
8 <sup>Gumlet is helping make open source sustainable by sponsoring Sindre Sorhus.</sup>
9 <a href="https://www.gumlet.com">
10 <div>
11 <img src="https://sindresorhus.com/assets/thanks/gumlet-logo.svg" width="300"/>
12 </div>
13 <sup><b>Optimised Image Delivery made simple</b></sup>
14 </a>
15</div>
16
17---
18
19
20## Install
21
22```
23$ npm install imagemin
24```
25
26
27## Usage
28
29```js
30const imagemin = require('imagemin');
31const imageminJpegtran = require('imagemin-jpegtran');
32const imageminPngquant = require('imagemin-pngquant');
33
34(async () => {
35 const files = await imagemin(['images/*.{jpg,png}'], {
36 destination: 'build/images',
37 plugins: [
38 imageminJpegtran(),
39 imageminPngquant({
40 quality: [0.6, 0.8]
41 })
42 ]
43 });
44
45 console.log(files);
46 //=> [{data: <Buffer 89 50 4e …>, destinationPath: 'build/images/foo.jpg'}, …]
47})();
48```
49
50
51## API
52
53### imagemin(input, options?)
54
55Returns `Promise<object[]>` in the format `{data: Buffer, sourcePath: string, destinationPath: string}`.
56
57#### input
58
59Type: `string[]`
60
61File paths or [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
62
63#### options
64
65Type: `object`
66
67##### destination
68
69Type: `string`
70
71Set the destination folder to where your files will be written. If no destination is specified, no files will be written.
72
73##### plugins
74
75Type: `Array`
76
77[Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
78
79##### glob
80
81Type: `boolean`<br>
82Default: `true`
83
84Enable globbing when matching file paths.
85
86### imagemin.buffer(buffer, options?)
87
88Returns `Promise<Buffer>`.
89
90#### buffer
91
92Type: `Buffer`
93
94Buffer to optimize.
95
96#### options
97
98Type: `object`
99
100##### plugins
101
102Type: `Array`
103
104[Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
105
106## Hosted API
107
108We also provide a hosted API for imagemin which may simplify your use case.
109
110<a href="https://imagemin.saasify.sh">
111 <img src="https://badges.saasify.sh?text=View%20Hosted%20API" height="40"/>
112</a>
113
114## Related
115
116- [imagemin-cli](https://github.com/imagemin/imagemin-cli) - CLI for this module
117- [imagemin-app](https://github.com/imagemin/imagemin-app) - GUI app for this module
118- [gulp-imagemin](https://github.com/sindresorhus/gulp-imagemin) - Gulp plugin
119- [grunt-contrib-imagemin](https://github.com/gruntjs/grunt-contrib-imagemin) - Grunt plugin