use new function to generate avatar

This commit is contained in:
printempw 2016-03-19 20:20:56 +08:00
parent a8cfdd3f01
commit 18997cc0ef

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:54:09
* @Last Modified time: 2016-03-19 20:19:53
*/
class Utils
@ -141,62 +141,30 @@ 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 int $size
* @param int $size
* @return null, will output directly
*/
public static function getAvatarFromSkin($username, $size) {
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);
$dest = imagecreatetruecolor(8, 8);
$final = imagecreatetruecolor($size, $size);
imagecopy($dest, $src, 0, 0, 8, 8, 8, 8);
// f => front, l => left, r => right, b => back
$x = array('f' => 8, 'l' => 16, 'r' => 0, 'b' => 24);
// 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);
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($final);
header('Content-type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($final);
imagedestroy($src);
imagedestroy($dest);
} else {
header('Content-Type: image/png');
echo Utils::fread(BASE_DIR."/assets/images/steve-avatar.png");