1 | # datauri
|
2 | [Module](http://npm.im/datauri) and [Client](http://npm.im/datauri-cli) to generate [Data URI scheme][datauri].
|
3 |
|
4 | [![Build Status](https://travis-ci.org/heldr/grunt-smushit.svg?branch=master)](http://travis-ci.org/heldr/datauri) [![Coverage Status](https://coveralls.io/repos/heldr/datauri/badge.svg?branch=master&service=github)](https://coveralls.io/github/heldr/datauri?branch=master) [![Dependency Status](https://www.versioneye.com/user/projects/560b7b3f5a262f001e0007e2/badge.svg?style=flat)](https://www.versioneye.com/user/projects/560b7b3f5a262f001e0007e2) [![NPM version](http://img.shields.io/npm/dm/datauri.svg?style=flat)](https://www.npmjs.org/package/datauri)
|
5 |
|
6 | ## MODULE
|
7 | `npm install --save datauri`
|
8 |
|
9 | ```js
|
10 | const Datauri = require('datauri');
|
11 | const datauri = new Datauri();
|
12 |
|
13 | datauri.on('encoded', function (content) {
|
14 | console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...";
|
15 | });
|
16 |
|
17 | datauri.on('error', function (content) {
|
18 | console.log('Fail!');
|
19 | });
|
20 |
|
21 | datauri.encode('test/myfile.png');
|
22 | ```
|
23 |
|
24 | ### Readable Stream
|
25 | ```js
|
26 | const Datauri = require('datauri');
|
27 | const datauri = new Datauri();
|
28 |
|
29 | datauri.pipe(process.stdout);
|
30 | datauri.encode('test/myfile.png');
|
31 | ```
|
32 |
|
33 | ### Promise (node 0.12+, works with es2016 async/await)
|
34 | ```js
|
35 | 'use strict';
|
36 |
|
37 | const DataURI = require('datauri').promise;
|
38 | // babelers: import { promise as DataURI } from 'datauri';
|
39 |
|
40 | DataURI('test/myfile.png')
|
41 | .then((content) => {
|
42 | console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
43 | }).catch((err) => {
|
44 | throw err;
|
45 | });
|
46 | ```
|
47 |
|
48 | ### Callback for vintage users
|
49 | ```js
|
50 | const DataURI = require('datauri');
|
51 | const datauri = new DataURI();
|
52 |
|
53 | datauri.encode('test/myfile.png', function (err, content) {
|
54 | if (err) {
|
55 | throw err;
|
56 | }
|
57 |
|
58 | console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
59 |
|
60 | console.log(this.mimetype); //=> "image/png"
|
61 | console.log(this.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
|
62 | console.log(this.getCSS()); //=> "\n.case {\n background-image: url('data:image/png;base64,iVBORw..."
|
63 | console.log(this.getCSS({
|
64 | class: "myClass",
|
65 | width: true,
|
66 | height: true
|
67 | })); //=> adds image width and height and custom class name
|
68 | });
|
69 |
|
70 | ```
|
71 |
|
72 | ### Create from a string
|
73 | ```js
|
74 | const DataURI = require('datauri');
|
75 | const datauri = new Datauri();
|
76 |
|
77 | datauri.format('.png', 'xkcd');
|
78 |
|
79 | console.log(datauri.content); //=> "data:image/png;base64,eGtjZA=="
|
80 | console.log(datauri.mimetype); //=> "image/png"
|
81 | console.log(datauri.base64); //=> "eGtjZA=="
|
82 | console.log(datauri.getCSS({
|
83 | class: "myClass",
|
84 | width: true,
|
85 | height: true
|
86 | })); //=> adds image width and height and custom class name
|
87 |
|
88 | ```
|
89 |
|
90 | ### Create from a Buffer
|
91 | If you already have your file as a Buffer, use this. It's much faster than passing a string.
|
92 |
|
93 | ```js
|
94 | var Datauri = require('datauri'),
|
95 | dUri = new Datauri();
|
96 |
|
97 | //...
|
98 | var buffer = fs.readFileSync('./hello');
|
99 | //...
|
100 |
|
101 | dUri.format('.png', buffer);
|
102 |
|
103 | console.log(dUri.content); //=> "data:image/png;base64,eGtjZA=="
|
104 | console.log(dUri.mimetype); //=> "image/png"
|
105 | console.log(dUri.base64); //=> "eGtjZA=="
|
106 | console.log(dUri.getCSS({
|
107 | class: "myClass",
|
108 | width: true,
|
109 | height: true
|
110 | })); //=> adds image width and height and custom class name
|
111 |
|
112 | ```
|
113 |
|
114 | #### Chaining all stuff
|
115 | ```js
|
116 | //...
|
117 | datauri
|
118 | .on('encoded', function (content) {
|
119 | console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
120 | console.log(this.mimetype); //=> "image/png"
|
121 | console.log(this.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
|
122 | console.log(this.getCSS()); //=> "\n.case {\n background-image: url('data:image/png;base64,iVBORw..."
|
123 | console.log(this.getCSS({
|
124 | class: "myClass"
|
125 | }); //=> "\n.myClass {\n background-image: url('data:image/png;base64,iVBORw..."
|
126 | })
|
127 | .on('error', function (content) {
|
128 | console.log('Fail!');
|
129 | })
|
130 | .encode('test/myfile.png');
|
131 | ```
|
132 |
|
133 | ### Sync (kids! Don't use it at home!)
|
134 |
|
135 | #### Sync Class
|
136 | If DataURI class is instanciated with a file path, the same will be processed synchronously.
|
137 |
|
138 | ```js
|
139 | const Datauri = require('datauri');
|
140 | let datauri = new Datauri('test/myfile.png');
|
141 |
|
142 | console.log(datauri.content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
143 | console.log(datauri.mimetype); //=> "image/png"
|
144 | console.log(datauri.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
|
145 | console.log(datauri.getCSS()); //=> "\n.case {\n background-image: url('data:image/png;base64,iVBORw..."
|
146 | console.log(datauri.getCSS("myClass")); //=> "\n.myClass {\n background-image: url('data:image/png;base64,iVBORw..."
|
147 | ```
|
148 |
|
149 | #### Sync Function
|
150 | ```js
|
151 | const Datauri = require('datauri').sync;
|
152 |
|
153 | console.log(Datauri('test/myfile.png')); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
154 | ```
|
155 | or for ES2015/6 lovers
|
156 |
|
157 | ```js
|
158 | import { sync as DataURI } from 'datauri';
|
159 |
|
160 | console.log(DataURI('test/myfile.png')); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
|
161 | ```
|
162 |
|
163 | NPM SCRIPT AND TERMINAL CLIENT
|
164 | -------
|
165 | * [datauri-cli](https://npmjs.org/package/datauri-cli)
|
166 |
|
167 |
|
168 | GULP
|
169 | -----
|
170 |
|
171 | * [gulp-image-data-uri](https://github.com/adam-lynch/gulp-image-data-uri) - A [Gulp](http://github.com/gulpjs/gulp) plugin for converting images to inline data-URIs. Intended to be a simple single-purpose wrapper for [heldr/datauri](https://github.com/heldr/datauri).
|
172 |
|
173 | GRUNT
|
174 | -----
|
175 |
|
176 | There are a bunch of grunt plugins running on top of datauri module.
|
177 |
|
178 | * [grunt-datauri](https://npmjs.org/package/grunt-datauri) - Create base64 encoded data-uris for css from images
|
179 | * [grunt-imweb](https://npmjs.org/package/grunt-imweb) - IMWEB Tasks Collection For Daily Workflow.
|
180 | * [grunt-static-inline](https://npmjs.org/package/grunt-static-inline) - A grunt plugin to replace url from static files such as img,js,css an put inline in a template.
|
181 | * [grunt-data-uri](https://npmjs.org/package/grunt-data-uri) - Convert to data-uri from image path.
|
182 | * [grunt-inline](https://npmjs.org/package/grunt-inline)
|
183 |
|
184 | DEVELOPING
|
185 | ----------
|
186 |
|
187 | ```CLI
|
188 | $ npm install
|
189 | $ npm run check
|
190 | ```
|
191 |
|
192 | To run test specs
|
193 |
|
194 | ```CLI
|
195 | $ npm run spec
|
196 | ```
|
197 |
|
198 | If you'd like to test the full process including npm installer, just run:
|
199 |
|
200 | ```CLI
|
201 | $ npm run fulltest
|
202 | ```
|
203 |
|
204 | ## Release notes
|
205 |
|
206 | * 1.0 - async by default, native promise, streams, split between datauri and datauri-cli package
|
207 | * 0.8 - remove node 0.8 support
|
208 | * 0.7 - generate css background-image instead of background shorthand
|
209 | * 0.6 - io.js support
|
210 | * 0.5 - Format data uri from a string
|
211 | * 0.4 - Promises support
|
212 | * 0.3 - API Rewritten from the top to the bottom + full async compatibility
|
213 | * 0.2 - Splitted in submodules mimer and templayed
|
214 | * 0.1 - First release
|
215 |
|
216 | ## License
|
217 |
|
218 | MIT License
|
219 | (c) [Helder Santana](http://heldr.com)
|
220 |
|
221 | [nodejs]: http://nodejs.org/download
|
222 | [iojs]: https://iojs.org/
|
223 | [datauri]: http://en.wikipedia.org/wiki/Data_URI_scheme
|
224 | [promisesaplus]: http://promises-aplus.github.io/promises-spec/
|