now you can get an avatar from your skin

This commit is contained in:
printempw 2016-03-19 20:01:16 +08:00
parent 82f6025ebc
commit a8cfdd3f01
6 changed files with 79 additions and 8 deletions

View File

@ -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 配置中说明。

View File

@ -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.');
}

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 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.'。');
}
}

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 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");
}
}
}

View File

@ -48,7 +48,7 @@
<ul class="dropdown-menu">
<!-- The user image in the menu -->
<li class="user-header">
<img src="../assets/images/steve-avatar.png" alt="User Image">
<img src="../get.php?type=avatar&uname=<?php echo $_SESSION['uname']; ?>&size=128" alt="User Image">
<p>
<?php echo $_SESSION['uname']; ?>
</p>
@ -77,7 +77,7 @@
<!-- Sidebar user panel (optional) -->
<div class="user-panel">
<div class="pull-left image">
<img src="../assets/images/steve-avatar.png" alt="User Image">
<img src="../get.php?type=avatar&uname=<?php echo $_SESSION['uname']; ?>&size=45" alt="User Image">
</div>
<div class="pull-left info">
<p><?php echo $_SESSION['uname']; ?></p>

View File

@ -47,7 +47,7 @@
<ul class="dropdown-menu">
<!-- The user image in the menu -->
<li class="user-header">
<img src="../assets/images/steve-avatar.png" alt="User Image">
<img src="../get.php?type=avatar&uname=<?php echo $_SESSION['uname']; ?>&size=128" alt="User Image">
<p>
<?php echo $_SESSION['uname']; ?>
</p>
@ -76,7 +76,7 @@
<!-- Sidebar user panel (optional) -->
<div class="user-panel">
<div class="pull-left image">
<img src="../assets/images/steve-avatar.png" alt="User Image">
<img src="../get.php?type=avatar&uname=<?php echo $_SESSION['uname']; ?>&size=45" alt="User Image">
</div>
<div class="pull-left info">
<p><?php echo $_SESSION['uname']; ?></p>