tweaked function of avatar generation

This commit is contained in:
printempw 2016-03-26 20:39:55 +08:00
parent 6abe1dbd87
commit 9dabe3bcc7
3 changed files with 38 additions and 27 deletions

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-02-02 20:56:42
* @Last Modified by: printempw
* @Last Modified time: 2016-03-26 18:47:48
* @Last Modified time: 2016-03-26 20:33:02
*
* All textures requests of legacy link will be handle here.
*/
@ -43,7 +43,7 @@ if (isset($_GET['type']) && isset($_GET['uname'])) {
}
} else if ($_GET['type'] == "avatar") {
$size = (isset($_GET['size']) && $_GET['size'] != "") ? (int)$_GET['size'] : 128;
Utils::getAvatarFromSkin($_GET['uname'], $size);
$user->getAvatar($size);
} else {
Utils::raise(1, 'Illegal parameters.');
}

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw
* @Last Modified time: 2016-03-19 19:01:11
* @Last Modified time: 2016-03-26 20:38:36
*/
use Database\Database;
@ -125,6 +125,28 @@ class User
}
}
/**
* Get avatar that generate from skin of user
*
* @param int $size [description]
* @param string $model, steve|alex
* @return null
*/
public function getAvatar($size, $model=null) {
if (is_null($model))
$model = ($this->getPreference() == "default") ? "steve" : "alex";
// output image directly
if ($this->getTexture($model) != "") {
$png = Utils::generateAvatarFromSkin($this->getTexture($model), $size);
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
} else {
header('Content-Type: image/png');
echo Utils::fread(BASE_DIR."/assets/images/steve-avatar.png");
}
}
public function setTexture($type, $file) {
// Remove the original texture first
if ($this->getTexture($type) != "")

View File

@ -3,7 +3,7 @@
* @Author: printempw
* @Date: 2016-01-16 23:01:33
* @Last Modified by: printempw
* @Last Modified time: 2016-03-19 20:19:53
* @Last Modified time: 2016-03-26 20:32:03
*/
class Utils
@ -142,34 +142,23 @@ class Utils
* Cut and resize to get avatar from skin
*
* @author https://github.com/jamiebicknell/Minecraft-Avatar/blob/master/face.php
* @param string $username
* @param string $hash
* @param int $size
* @return null, will output directly
* @return resource
*/
public static function getAvatarFromSkin($username, $size, $view='f') {
$user = new User($username);
$model_preferrnce = ($user->getPreference() == "default") ? "steve" : "alex";
if ($user->getTexture($model_preferrnce) != "") {
$src = imagecreatefrompng(BASE_DIR."/textures/".$user->getTexture($model_preferrnce));
$dest = imagecreatetruecolor($size, $size);
public static function generateAvatarFromSkin($hash, $size, $view='f') {
$src = imagecreatefrompng(BASE_DIR."/textures/$hash");
$dest = imagecreatetruecolor($size, $size);
// f => front, l => left, r => right, b => back
$x = array('f' => 8, 'l' => 16, 'r' => 0, 'b' => 24);
// f => front, l => left, r => right, b => back
$x = array('f' => 8, 'l' => 16, 'r' => 0, 'b' => 24);
imagecopyresized($dest, $src, 0, 0, $x[$view], 8, $size, $size, 8, 8); // Face
imagecolortransparent($src, imagecolorat($src, 63, 0)); // Black Hat Issue
imagecopyresized($dest, $src, 0, 0, $x[$view] + 32, 8, $size, $size, 8, 8); // Accessories
header('Content-type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
} else {
header('Content-Type: image/png');
echo Utils::fread(BASE_DIR."/assets/images/steve-avatar.png");
}
imagecopyresized($dest, $src, 0, 0, $x[$view], 8, $size, $size, 8, 8); // Face
imagecolortransparent($src, imagecolorat($src, 63, 0)); // Black Hat Issue
imagecopyresized($dest, $src, 0, 0, $x[$view] + 32, 8, $size, $size, 8, 8); // Accessories
imagedestroy($src);
return $dest;
}
}