Created
April 12, 2017 01:39
-
-
Save ragingprodigy/2d9d46198a7db595a5916256a7875482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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