Last active
April 13, 2016 08:34
-
-
Save misterdev/f1a496b1bbe90dc804a235465dd8fd23 to your computer and use it in GitHub Desktop.
URL to DataURI converter for NodeJS without Canvas
This file contains hidden or 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
/* | |
* Based on https://gist.github.com/583836 from http://stackoverflow.com/questions/3709391/node-js-base64-encode-a-downloaded-image-for-use-in-data-uri. | |
* / | |
var http = require('http') | |
var Stream = require('stream').Transform | |
http.request(url, function(response) { | |
var data = new Stream(); | |
response.on('data', function(chunk) { | |
data.push(chunk); | |
}); | |
response.on('end', function() { | |
var data_uri_prefix = "data:" + response.headers["content-type"] + ";base64,"; | |
var image = new Buffer(data.read(), 'binary').toString('base64'); | |
var data_uri = data_uri_prefix + image; | |
}); | |
}).end(); | |
/* | |
* Other similar resources: | |
* https://gist.github.com/jfsiii/803410 | |
* https://gist.github.com/oliyh/db3d1a582aefe6d8fee9 | |
* http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment