Skip to content

Commit

Permalink
added option to encode a local file
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoba authored and riyadhalnur committed Sep 17, 2015
1 parent db2050e commit c9cd5df
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
node-base64-image
=================

Download images from remote URLs and encode/decode them to base64
Download images from remote URLs or use local images and encode/decode them to base64

To install
`npm install node-base64-image --save`
Expand All @@ -26,9 +26,22 @@ base64.base64encode('www.someurl.com/image.jpg', function (err, image) {
});
```

#### Encode a local image
```
var path = __dirname + '/../test.jpg',
options = {localFile: true, string: true};
base64.base64encode(path, function (err, image) {
if (err) { console.log(err); }
console.log(image);
});
```


##### Parameters
url (string) - the url of the image to be downloaded and encoded.
options (object) - if string is passed is with 'true', the image returned will be a base64 string. Otherwise, the base64 buffer is returned.
if localFile is passed is with 'true', a local image instead of a remote one will be used
callback (function) - the callback will contain the err object and the encoded image object.

#### Decode and write a base64 encoded image to disk
Expand All @@ -42,7 +55,7 @@ base64.base64decoder(imageData, options, function (err, saved) {
});
```

##### Paramaters
##### Parameters
imageData (buffer) - the base64 image buffer.
options (object) - contains the 'filename' property; this will be the written image file.
callback (function) - the callback will contain the err object and the 'successful save' string.
Expand Down
50 changes: 35 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,49 @@ var base64encoder = function (url, options, callback) {
throw new Error('URL cannot be empty!');
}

request({url: url, encoding: null}, function (err, res, body) {
if (err) { return callback(err); }

if (body && res.statusCode === 200) {
var image;

if (options && options.string === true) {
image = body.toString('base64');
return callback(null, image);
} else {
image = new Buffer(body, 'base64');
return callback(null, image);
}
var encoder = function (body, options) {
var image;

if (options && options.string === true) {
image = body.toString('base64');
return callback(null, image);
} else {
image = new Buffer(body, 'base64');
return callback(null, image);
}
});
};

if (options && options.localFile === true) {
fs.readFile(url, function (err, data) {
if (err) {
return callback(err);
}

return encoder(data, options);
});
}
else {
request({url: url, encoding: null}, function (err, res, body) {
if (err) {
return callback(err);
}

if (body && res.statusCode === 200) {
return encoder(body, options);
}
});
}

};

var base64decoder = function (imageBuffer, options, callback) {
options = options || {};

if (options && options.filename) {
fs.writeFile(options.filename + '.jpg', imageBuffer, 'base64', function (err) {
if (err) { return callback(err); }
if (err) {
return callback(err);
}
return callback(null, 'Image saved successfully to disk!');
});
}
Expand Down
33 changes: 33 additions & 0 deletions test/tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9cd5df

Please sign in to comment.