base64 = function (filename, callback) { if (!callback) callback = util.noop; fs.readFile(filename, function(err, data) { if (err) return callback(err); callback(null, base64(filename, data)); }); }
...
npm install base64-img --save
```
## test
```
mocha
```
## API
### .base64(filename, callback)
Convert image file to image base64 data
* {string} ``filename`` required
The image path
* {function} ``callback(err, data)`` required
Callback with image base64 data
```js
base64Img.base64('path/demo.png', function(err, data) {})
...
base64Sync = function (filename) { var data = fs.readFileSync(filename); return base64(filename, data); }
...
The image path
* {function} ``callback(err, data)`` required
Callback with image base64 data
```js
base64Img.base64('path/demo.png', function(err, data) {})
```
### .base64Sync(filename)
The api same as base64, but it's synchronous
```js
var data = base64Img.base64Sync('path/demo.png');
```
### .requestBase64(url, callback)
* {string} ``url`` required
...
img = function (data, destpath, name, callback) { var result = img(data); var filepath = path.join(destpath, name + result.extname); fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function(err) { callback(err, filepath); }); }
...
```js
var url = 'http://../demo.png';
base64Img.requestBase64(url, function(err, res, body) {
});
```
### .img(data, destpath, name, callback)
Convert image base64 data to image
* {string} ``data`` required
Image base64 data
* {string} ``destpath`` required
Dest path, if the destpath is root, pass empty string
* {string} ``name`` required
The image's filename
...
imgSync = function (data, destpath, name) { var result = img(data); var filepath = path.join(destpath, name + result.extname); fs.writeFileSync(filepath, result.base64, { encoding: 'base64' }); return filepath; }
...
* {string} ``name`` required
The image's filename
* {function} ``callback(err, filepath)`` required
```js
base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {});
```
### .imgSync(data, destpath, name)
The api same as img, but it's synchronous
```js
var filepath = base64Img.imgSync('data:image/png;base64,...', '', '2');
```
...
requestBase64 = function (url, callback) { request({ url: url, isBuffer: true }, function (err, res, body) { if (err) return callback(err); var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64'); callback(err, res, data); }); }
...
### .base64Sync(filename)
The api same as base64, but it's synchronous
```js
var data = base64Img.base64Sync('path/demo.png');
```
### .requestBase64(url, callback)
* {string} ``url`` required
* {function} ``callback(err, res, body)`` required
Callback with http request
```js
var url = 'http://../demo.png';
base64Img.requestBase64(url, function(err, res, body) {
...