Skip to content

Instantly share code, notes, and snippets.

@dariusc93
Created November 18, 2013 06:02
Show Gist options
  • Save dariusc93/7523275 to your computer and use it in GitHub Desktop.
Save dariusc93/7523275 to your computer and use it in GitHub Desktop.
app.get('/img/:x/:y/:name', function(req,res){
var x = req.params.x,
y = req.params.y;
var sub = req.params.name.split('.'),
name = sub[sub.length-2],
ext = sub[sub.length-1];
var d1 = ['/path/to/',name,'.',ext];
fs.exists(d1.join(""), function(exist){
if(exist){
var image = Canvas.Image,
img = new image;
img.onerror = function(err){
throw err;
};
img.onload = function(){
var canvas = new Canvas(x,y);
var ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = true;
ctx.drawImage(img,0,0,x,y);
canvas.toBuffer(function(err,buf){
res.writeHead(200, {'Content-Type': 'image/'+ext });
res.end(buf,'binary');
});
};
img.src = d1.join("");
}else{
res.status(404);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment