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", "serve-favicon": "~2.1.3",
"debug": "~2.0.0", "debug": "~2.0.0",
"jade": "~1.6.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') var fs = require('fs')
/* GET home page. */ /* GET home page. */
router.get('/:uuid', function(req, res) { router.get('/:uuid/:size?', function(req, res) {
//res.render('index', { title: 'Express' }); //res.render('index', { title: 'Express' });
//res.send("uuid is set to " + req.param("uuid")); //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"; var filename = 'skins/' + uuid + ".png";
if (fs.existsSync(filename)) { if (fs.existsSync(filename)) {
fs.readFile(filename, function(err, data) { skins.extract_face(filename, size, function() {
res.writeHead(200, {'Content-Type': 'image/jpeg'}); skins.extract_face(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(data); res.end(data);
}); });
});
} else { } else {
skins.get_profile(uuid, function(profile) { skins.get_profile(uuid, function(profile) {
var skinurl = skins.skin_url(profile); var skinurl = skins.skin_url(profile);
if (skinurl) { if (skinurl) {
skins.skin_file(skinurl, filename, function() { skins.skin_file(skinurl, filename, function() {
skins.extract_face(filename, filename, function() { skins.extract_face(filename, size, function(data) {
fs.readFile(filename, function(err, data) { res.writeHead(200, {'Content-Type': 'image/png'});
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end(data); res.end(data);
}); });
}); });
});
} else { } 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 http = require('http');
var https = require('https'); var https = require('https');
var fs = require('fs'); var fs = require('fs');
var imagemagick = require('imagemagick'); var lwip = require('lwip');
/* /*
@ -11,11 +11,18 @@ var imagemagick = require('imagemagick');
module.exports = { module.exports = {
get_profile: function(uuid, callback) { get_profile: function(uuid, callback) {
https.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid, function(res) { 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) { res.on('data', function(d) {
var profile = JSON.parse(d); var profile = JSON.parse(d);
if (profile.error) throw profile.error; if (profile.error) callback(null);
callback(profile); callback(profile);
}); });
}).on('error', function(e) {
console.error(e);
}); });
}, },
@ -44,10 +51,14 @@ module.exports = {
}); });
}); });
}, },
extract_face: function(infile, outfile, callback) { extract_face: function(infile, size, callback) {
imagemagick.convert([infile, '-crop', '8x8+8+8', outfile], function(err, stdout) { lwip.open(infile, function(err, image){
if (err) throw err; image.batch()
callback(); .crop(8,8,15,15)
.resize(size, size, "nearest-neighbor")
.toBuffer('png', function(err, buffer){
callback(buffer);
});
}); });
} }
}; };