From 9dabe3bcc728406d0e38992e562b54d345e38f3b Mon Sep 17 00:00:00 2001 From: printempw Date: Sat, 26 Mar 2016 20:39:55 +0800 Subject: [PATCH] tweaked function of avatar generation --- get.php | 4 ++-- libraries/User.class.php | 24 +++++++++++++++++++++++- libraries/Utils.class.php | 37 +++++++++++++------------------------ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/get.php b/get.php index c50cff0b..14561ee8 100644 --- a/get.php +++ b/get.php @@ -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.'); } diff --git a/libraries/User.class.php b/libraries/User.class.php index b1850edd..f6e5a41a 100644 --- a/libraries/User.class.php +++ b/libraries/User.class.php @@ -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) != "") diff --git a/libraries/Utils.class.php b/libraries/Utils.class.php index 52b88d44..5067cc51 100644 --- a/libraries/Utils.class.php +++ b/libraries/Utils.class.php @@ -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; } }