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
|
30 | const imagemin = require('imagemin');
|
31 | const imageminJpegtran = require('imagemin-jpegtran');
|
32 | const 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 |
|
55 | Returns `Promise<object[]>` in the format `{data: Buffer, sourcePath: string, destinationPath: string}`.
|
56 |
|
57 | #### input
|
58 |
|
59 | Type: `string[]`
|
60 |
|
61 | File paths or [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
|
62 |
|
63 | #### options
|
64 |
|
65 | Type: `object`
|
66 |
|
67 | ##### destination
|
68 |
|
69 | Type: `string`
|
70 |
|
71 | Set the destination folder to where your files will be written. If no destination is specified, no files will be written.
|
72 |
|
73 | ##### plugins
|
74 |
|
75 | Type: `Array`
|
76 |
|
77 | [Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
|
78 |
|
79 | ##### glob
|
80 |
|
81 | Type: `boolean`<br>
|
82 | Default: `true`
|
83 |
|
84 | Enable globbing when matching file paths.
|
85 |
|
86 | ### imagemin.buffer(buffer, options?)
|
87 |
|
88 | Returns `Promise<Buffer>`.
|
89 |
|
90 | #### buffer
|
91 |
|
92 | Type: `Buffer`
|
93 |
|
94 | Buffer to optimize.
|
95 |
|
96 | #### options
|
97 |
|
98 | Type: `object`
|
99 |
|
100 | ##### plugins
|
101 |
|
102 | Type: `Array`
|
103 |
|
104 | [Plugins](https://www.npmjs.com/browse/keyword/imageminplugin) to use.
|
105 |
|
106 | ## Hosted API
|
107 |
|
108 | We 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
|