I want get a image from url and encode base64 that image by nodejs, then show that image by base64 encode, but this code is incorrect. this code save incorrect png file.
var http = require('http')
, fs = require('fs')
, options
options = {
host: 'google.com'
, port: 80
, path: '/images/srpr/logo3w.png'
}
function base64_encode(bitmap) {
return new Buffer(bitmap).toString('base64');
}
function ImageReady(res2){
var request = http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk;
})
res.on('end', function(){
var base64encode = base64_encode(imagedata);
res2.end('<img src="data:image/png;base64,'+base64encode+'" />');
fs.writeFile('logo.png', imagedata, 'binary', function(err){
if (err) throw err
console.log('File saved.')
})
})
})
}
var httpListen = require('http');
httpListen.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
ImageReady(res);
}).listen(8080);
console.log('Server running!');
Try this:
var loadBase64Image = function (url, callback) {
// Required 'request' module
var request = require('request');
// Make request to our image url
request({url: url, encoding: null}, function (err, res, body) {
if (!err && res.statusCode == 200) {
// So as encoding set to null then request body became Buffer object
var base64prefix = 'data:' + res.headers['content-type'] + ';base64,'
, image = body.toString('base64');
if (typeof callback == 'function') {
callback(image, base64prefix);
}
} else {
throw new Error('Can not download image');
}
});
};
using somewhere in your node.js application:
// ...
loadBase64Image('http://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Carcharhinus_falciformis_off_Cuba.jpg/180px-Carcharhinus_falciformis_off_Cuba.jpg', function (image, prefix) {
res.send('<img src="' + prefix + image + '"') />;
});
// ...
Try using request npm plugin,
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+'"/>');
}
});
}
});
More info: https://gist.github.com/583836
I tried to use @codef0rmer code, but no success... after reading gist comments I stumbled upon a solution that doesn't depends on "BufferList" external library and worked as a charm...
So heres the link for this gist! Its written in Coffee, all credits to @hackable:
express = require("express")
request = require("request")
BufferList = require("bufferlist").BufferList //no longer needed
app = express.createServer(express.logger(), express.bodyParser())
app.get "/", (req, res) ->
if req.param("url")
url = unescape(req.param("url"))
request
uri: url
encoding: 'binary'
, (error, response, body) ->
if not error and response.statusCode is 200
data_uri_prefix = "data:" + response.headers["content-type"] + ";base64,"
image = new Buffer(body.toString(), "binary").toString("base64")
image = data_uri_prefix + image
res.send "<img src=\"" + image + "\"/>"
app.listen 3000