Skip to content

Instantly share code, notes, and snippets.

@ragingprodigy
Created April 12, 2017 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragingprodigy/2d9d46198a7db595a5916256a7875482 to your computer and use it in GitHub Desktop.
Save ragingprodigy/2d9d46198a7db595a5916256a7875482 to your computer and use it in GitHub Desktop.
function encodeImage(imageUrl, callback) {
var URL = require('url'),
sURL = imageUrl,
oURL = URL.parse(sURL),
http = require('http');
var request = http.request({method:'GET', path:oURL.pathname, host: oURL.hostname, port:80});
request.end();
request.on('response', function (response) {
var type = response.headers["content-type"],
prefix = "data:" + type + ";base64,",
body = "";
response.setEncoding('binary');
response.on('end', function () {
var base64 = new Buffer(body, 'binary').toString('base64'),
data = prefix + base64;
return callback(data);
});
response.on('data', function (chunk) {
if (response.statusCode == 200) body += chunk;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment