Skip to content

Instantly share code, notes, and snippets.

@misterdev
Last active April 13, 2016 08:34
Show Gist options
  • Save misterdev/f1a496b1bbe90dc804a235465dd8fd23 to your computer and use it in GitHub Desktop.
Save misterdev/f1a496b1bbe90dc804a235465dd8fd23 to your computer and use it in GitHub Desktop.
URL to DataURI converter for NodeJS without Canvas
/*
* 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