Skip to content

Instantly share code, notes, and snippets.

@bxjx
Created September 17, 2010 06:51
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save bxjx/583836 to your computer and use it in GitHub Desktop.
Save bxjx/583836 to your computer and use it in GitHub Desktop.
var express = require('express'),
request = require('request'),
BufferList = require('bufferlist').BufferList,
sys = require('sys');
var app = express.createServer(
express.logger(),
express.bodyDecoder()
);
app.get('/', function(req, res){
if(req.param("url")) {
var url = unescape(req.param("url"));
var bl = new BufferList();
request({uri:url, responseBodyStream: bl}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data_uri_prefix = "data:" + response.headers["content-type"] + ";base64,";
var image = new Buffer(bl.toString(), 'binary').toString('base64');
image = data_uri_prefix + image;
res.send('<img src="'+image+'"/>');
}
});
}
});
app.listen(3010);
@jfsiii
Copy link

jfsiii commented Jan 31, 2011

Take a look at https://gist.github.com/804225

I ditched the external dependencies (BufferList and request) and was able to encode the image using only the standard HTTP and URL modules

While I'm curious what I was doing wrong, I'm happier with this approach. Let me know if you see any areas for improvement. Specifically, how I can skip the step of assembling the binary string and jump straight to converting it to base64.

@hackable
Copy link

this https://gist.github.com/1294667 should work fine

the current example above doesn't work due to dericated modules in the request

try by putting

http://localhost:3000/?url=[url_of_your_image]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment