Use lwip, add size

This commit is contained in:
Jake 2014-10-19 11:13:29 -05:00
parent 44f54691ea
commit ec393a47ed
3 changed files with 42 additions and 25 deletions

View File

@ -13,6 +13,6 @@
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"imagemagick": "0.1.3"
"lwip": "0.0.5"
}
}

View File

@ -4,30 +4,36 @@ var skins = require('../skins');
var fs = require('fs')
/* GET home page. */
router.get('/:uuid', function(req, res) {
router.get('/:uuid/:size?', function(req, res) {
//res.render('index', { title: 'Express' });
//res.send("uuid is set to " + req.param("uuid"));
uuid = req.param('uuid')
//console.log(req.param('size'))
var uuid = req.param('uuid')
var size = req.param('size')
if (size == null) {
size = 180;
}
var filename = 'skins/' + uuid + ".png";
if (fs.existsSync(filename)) {
fs.readFile(filename, function(err, data) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
skins.extract_face(filename, size, function() {
skins.extract_face(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(data);
});
});
} else {
skins.get_profile(uuid, function(profile) {
var skinurl = skins.skin_url(profile);
if (skinurl) {
skins.skin_file(skinurl, filename, function() {
skins.extract_face(filename, filename, function() {
fs.readFile(filename, function(err, data) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
skins.extract_face(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(data);
});
});
});
} else {
res.send("No skin found.");
res.status(404) // HTTP status 404: NotFound
.send('404 Not found')
}
});
}

View File

@ -1,7 +1,7 @@
var http = require('http');
var https = require('https');
var fs = require('fs');
var imagemagick = require('imagemagick');
var lwip = require('lwip');
/*
@ -11,11 +11,18 @@ var imagemagick = require('imagemagick');
module.exports = {
get_profile: function(uuid, callback) {
https.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid, function(res) {
if (res.statusCode == "204") {
callback(null);
return null;
}
res.on('data', function(d) {
var profile = JSON.parse(d);
if (profile.error) throw profile.error;
if (profile.error) callback(null);
callback(profile);
});
}).on('error', function(e) {
console.error(e);
});
},
@ -44,10 +51,14 @@ module.exports = {
});
});
},
extract_face: function(infile, outfile, callback) {
imagemagick.convert([infile, '-crop', '8x8+8+8', outfile], function(err, stdout) {
if (err) throw err;
callback();
extract_face: function(infile, size, callback) {
lwip.open(infile, function(err, image){
image.batch()
.crop(8,8,15,15)
.resize(size, size, "nearest-neighbor")
.toBuffer('png', function(err, buffer){
callback(buffer);
});
});
}
};