Skip to content

Commit

Permalink
add requestBase64 api
Browse files Browse the repository at this point in the history
  • Loading branch information
wliao authored and wliao committed Mar 25, 2015
1 parent 28f045f commit 66901bf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ The api same as base64, but it's synchronous
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) {

});
```

### .img(data, destpath, name, callback)
Convert image base64 data to image
* {string} ``data`` required
Expand Down
24 changes: 24 additions & 0 deletions base64-img.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('file-system');
var path = require('path');
var request = require('ajax-request');

function base64(filename, data) {
var extname = path.extname(filename).substr(1);
Expand Down Expand Up @@ -55,6 +56,29 @@ exports.base64Sync = function(filename) {
return base64(filename, data);
};

/**
* @description
* Get base64 from url
* @example
* request.base64(
* 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png',
* function(err, res, body) {
*
* }
* );
*/
exports.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);
});
};

/**
* @description
* Convert image base64 data to img
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "base64-img",
"version": "1.0.1",
"version": "1.0.2",
"description": "Convert img to base64, or convert base64 to img",
"main": "base64-img.js",
"scripts": {
Expand All @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/douzi8/base64-img",
"dependencies": {
"ajax-request": "^1.1.0",
"file-system": "^2.1.0"
},
"devDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,21 @@ describe('Test', function() {
var filepath = base64Img.imgSync(data, getpath('dest'), '3');
assert.equal('.jpg', path.extname(filepath));
});

it('request', function(done) {
var url = 'http://webresource.c-ctrip.com/ResCRMOnline/R5/html5/images/57.png';
var demo = readFileSync('img/demo.png');
var data = base64Img.base64Sync(getpath('img/demo.png'));

base64Img.requestBase64(url, function(err, res, body) {
if (err) {
done();
return console.log(err);
}

assert.equal(body.substr(0, 10), data.substr(0, 10));
done();
});
});
});
});

0 comments on commit 66901bf

Please sign in to comment.