diff --git a/README.md b/README.md index b31dd2f5..5f637630 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ rewrite ^/(skin|cape)/([^/-]*)(|-)(|alex|steve).png$ /get.php?type=$1&model=$4&u # 以下是可选内容 rewrite ^/(usm|csl)/([^/]*).json$ /get.php?type=json&uname=$2&api=$1 last; rewrite ^/(usm|csl)/textures/(.*)$ /textures/$2 last; +# 用于获取皮肤头像 +rewrite ^/avatar/(|[0-9]*/)([^/-]*).png$ /get.php?type=avatar&uname=$2&size=$1 last; ``` 你可以使用可选的重写规则来同时支持 CustomSkinLoader API 和 UniSkinAPI。如何同时支持会在下面 Mod 配置中说明。 diff --git a/get.php b/get.php index 45272949..f13cebcd 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-19 10:06:25 + * @Last Modified time: 2016-03-19 19:53:57 * * All textures requests of legacy link will be handle here. */ @@ -41,6 +41,9 @@ if (isset($_GET['type']) && isset($_GET['uname'])) { } else { echo $user->getJsonProfile(Config::get('api_type')); } + } else if ($_GET['type'] == "avatar") { + $size = (isset($_GET['size']) && $_GET['size'] != "") ? (int)$_GET['size'] : 128; + Utils::getAvatarFromSkin($_GET['uname'], $size); } else { Utils::raise(1, 'Illegal parameters.'); } diff --git a/includes/classes/User.class.php b/includes/classes/User.class.php index 595f60c2..b1850edd 100644 --- a/includes/classes/User.class.php +++ b/includes/classes/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 10:54:07 + * @Last Modified time: 2016-03-19 19:01:11 */ use Database\Database; @@ -121,7 +121,7 @@ class User } } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); - Utils::showErrorPage(404, '该用户尚未上传请求的贴图类型。'); + Utils::showErrorPage(404, '该用户尚未上传请求的贴图类型 '.$type.'。'); } } diff --git a/includes/classes/Utils.class.php b/includes/classes/Utils.class.php index 934a07d4..63d16a28 100644 --- a/includes/classes/Utils.class.php +++ b/includes/classes/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 12:55:08 + * @Last Modified time: 2016-03-19 19:54:09 */ class Utils @@ -138,4 +138,70 @@ class Utils return false; } + /** + * Cut and resize to get avatar from skin + * + * @param string $username + * @param int $size + * @return null, will output directly + */ + public static function getAvatarFromSkin($username, $size) { + $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(8, 8); + $final = imagecreatetruecolor($size, $size); + imagecopy($dest, $src, 0, 0, 8, 8, 8, 8); + + // Get image width and height + $w = imagesx($src); + $h = imagesy($src); + // Turn alpha blending off + imagealphablending($src, false); + // Find the most opaque pixel in the image (the one with the smallest alpha value) + $minalpha = 127; + for($x = 0; $x < $w; $x++) + for($y = 0; $y < $h; $y++) { + $alpha = (imagecolorat($src, $x, $y) >> 24) & 0xFF; + if ($alpha < $minalpha) { + $minalpha = $alpha; + } + } + // loop through image pixels and modify alpha for each + for($x = 0; $x < $w; $x++) { + for($y = 0; $y < $h; $y++) { + // get current alpha value (represents the TANSPARENCY!) + $colorxy = imagecolorat($src, $x, $y); + $alpha = ($colorxy >> 24) & 0xFF; + // calculate new alpha + if ($minalpha !== 127) { + $alpha = 127 + 127 * 1 * ($alpha - 127) / (127 - $minalpha); + } else { + $alpha += 127 * 1; + } + // get the color index with new alpha + $alphacolorxy = imagecolorallocatealpha($src, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha); + // set pixel with the new color + opacity + imagesetpixel($src, $x, $y, $alphacolorxy); + } + } + // The image copy + imagecopy($dest, $src, 0, 0, 56, 8, 8, 8); + imagecopyresized($final, $dest, 0, 0, 0, 0, $size, $size, 8, 8); + + header('Content-Type: image/png'); + imagepng($final); + + imagedestroy($dest); + imagedestroy($final); + imagedestroy($src); + } else { + header('Content-Type: image/png'); + echo Utils::fread(BASE_DIR."/assets/images/steve-avatar.png"); + } + + } + } diff --git a/templates/admin/header.tpl.php b/templates/admin/header.tpl.php index 2f464ac3..266946b2 100644 --- a/templates/admin/header.tpl.php +++ b/templates/admin/header.tpl.php @@ -48,7 +48,7 @@