Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for promises #7

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 67 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,99 @@
# base64-img
# base64-img-promise

Convert img to base64, or convert base64 to img

```js
var base64Img = require('base64-img');
var base64Img = require('base64-img-promise');
```

## install

```
npm install base64-img --save
npm install base64-img-promise --save
```

## test

```
mocha
```

## API
### .base64(filename, callback)

Note: All methods which take a callback, if omitted, will return a promise

### .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

* {string} `filename` required
The image path
* {function} `callback(err, data)` optional
Callback with image base64 data

```js
base64Img.base64('path/demo.png', function(err, data) {})
/* promise */
base64Img.base64('path/demo.png').then(function(data) {});

/* callback */
base64Img.base64('path/demo.png', function(err, data) {});
```

### .base64Sync(filename)
The api same as base64, but it's synchronous

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
### .requestBase64(url[, callback])

* {string} `url` required
* {function} `callback(err, res, body)` optional
Callback with http request

```js
/* promise */
var url = 'http://../demo.png';
base64Img.requestBase64(url, function(err, res, body) {

});
base64Img.requestBase64(url).then(function({ res, data }) {});

var url = 'http://../demo.png';
base64Img.requestBase64(url, function(err, res, data) {});
```

### .img(data, destpath, name, callback)
### .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
* {function} ``callback(err, filepath)`` required

* {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
* {function} `callback(err, filepath)` optional

```js
base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {});
/* promise */
base64Img
.img('data:image/png;base64,...', 'dest', '1')
.then(function(filepath) {});

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');
```
```

---

This is a fork of https://github.com/douzi8/base64-img
70 changes: 61 additions & 9 deletions base64-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var request = require('ajax-request');
function base64(filename, data) {
var extname = path.extname(filename).substr(1);
extname = extname || 'png';

return 'data:image/' + extname + ';base64,' + data.toString('base64');
}

Expand All @@ -28,16 +28,56 @@ function img(data) {
};
}

function base64Promise(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, function (err, data) {
if (err) reject(err);

resolve(base64(filename, data));
});
});
};

function requestBase64Promise(url) {
return new Promise((resolve, reject) => {
request({
url: url,
isBuffer: true
}, function (err, res, body) {
if (err) return reject(err);

var data = 'data:' + res.headers['content-type'] + ';base64,' + body.toString('base64');
resolve({ res, data });
});
});
};

function imgPromise(data, destpath, name) {
return new Promise((resolve, reject) => {
var result = img(data);
var filepath = path.join(destpath, name + result.extname);

fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) {
if (err) return reject(err);
resolve(filepath);
});
});
};

/**
* @description
* Get image file base64 data
* @example
* base64Img.base64('path/demo.png', function(err, data) {})
* @example
* base64Img.base64('path/demo.png').then(function(data) {
*
* });
*/
exports.base64 = function(filename, callback) {
if (!callback) callback = util.noop;
exports.base64 = function (filename, callback) {
if (!callback) return base64Promise(filename);

fs.readFile(filename, function(err, data) {
fs.readFile(filename, function (err, data) {
if (err) return callback(err);

callback(null, base64(filename, data));
Expand All @@ -50,7 +90,7 @@ exports.base64 = function(filename, callback) {
* @example
* var data = base64Img.base64Sync('path/demo.png');
*/
exports.base64Sync = function(filename) {
exports.base64Sync = function (filename) {
var data = fs.readFileSync(filename);

return base64(filename, data);
Expand All @@ -66,8 +106,16 @@ exports.base64Sync = function(filename) {
*
* }
* );
* @example
* request.base64(
* 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png',
* ).then(function({res, data}) {
*
* });
*/
exports.requestBase64 = function(url, callback) {
exports.requestBase64 = function (url, callback) {
if (!callback) return requestBase64Promise(url);

request({
url: url,
isBuffer: true
Expand All @@ -84,12 +132,16 @@ exports.requestBase64 = function(url, callback) {
* Convert image base64 data to img
* @example
* base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {});
* @example
* base64Img.img('data:image/png;base64,...', 'dest', '1').then(function(filepath) {});
*/
exports.img = function(data, destpath, name, callback) {
exports.img = function (data, destpath, name, callback) {
if (!callback) return imgPromise(data, destpath, name);

var result = img(data);
var filepath = path.join(destpath, name + result.extname);

fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function(err) {
fs.writeFile(filepath, result.base64, { encoding: 'base64' }, function (err) {
callback(err, filepath);
});
};
Expand All @@ -100,7 +152,7 @@ exports.img = function(data, destpath, name, callback) {
* @example
* var filepath = base64Img.imgSync('data:image/png;base64,...', 'dest', '1');
*/
exports.imgSync = function(data, destpath, name) {
exports.imgSync = function (data, destpath, name) {
var result = img(data);
var filepath = path.join(destpath, name + result.extname);

Expand Down
Loading