Created
September 12, 2013 15:20
-
-
Save MartinMuzatko/6539269 to your computer and use it in GitHub Desktop.
Minecraft Avatar Viewhelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TYPO3\Mcserver\ViewHelper; | |
use \TYPO3\CMS\Mcserver\Utility as McUtility; | |
/** | |
* This class is a demo view helper for the Fluid templating engine. | |
* | |
* @author Martin Muzatko | |
* @package mcserver | |
* | |
*/ | |
class AvatarViewHelper | |
extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper | |
{ | |
/** | |
* Viewhelper for rendering Avatars | |
* | |
* @param string $user user | |
* @param string $size size | |
* @return mixed Transformed Value | |
*/ | |
public function render($user = NULL, $size = 32) { | |
$minecraftAvatar = McUtility\MinecraftAvatar::getInstance(); | |
$user = $this->arguments['user']; | |
$size = $this->arguments['size']; | |
return '<img src="'.$minecraftAvatar->getAvatar($user, $size).'" alt="'.$user.'">'; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TYPO3\CMS\Mcserver\Utility; | |
class MinecraftAvatarException extends \Exception | |
{ | |
// Exception thrown by MinecraftQuery class | |
} | |
class MinecraftAvatar | |
{ | |
/* | |
* Originally written by xPaw | |
* Modifications and additions by ivkos | |
* | |
* GitHub: https://github.com/ivkos/Minecraft-Query-for-PHP | |
*/ | |
protected $size = 32; | |
const URL = 'http://s3.amazonaws.com/MinecraftSkins/'; | |
public function setSize($size) | |
{ | |
$this->size = $size; | |
} | |
public function getSize() | |
{ | |
return $this->size; | |
} | |
public function getAvatar($user, $size) | |
{ | |
if (isset($size)) | |
{ | |
$size = $this->getSize(); | |
} | |
else | |
{ | |
$this->setSize($size); | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, self::URL . $user . '.png'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$output = curl_exec($ch); | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if($status!='200') | |
{ | |
$output = $this->getDefault(); | |
} | |
$this->render($output); | |
} | |
public function render($skin) | |
{ | |
$size = $this->getSize(); | |
$im = imagecreatefromstring($skin); | |
$av = imagecreatetruecolor($size,$size); | |
imagecopyresized($av,$im,0,0,8,8,$size,$size,8,8); // Face | |
imagecolortransparent($im,imagecolorat($im,63,0)); // Black Hat Issue | |
imagecopyresized($av,$im,0,0,40,8,$size,$size,8,8); // Accessories | |
imagepng($av); | |
imagedestroy($im); | |
imagedestroy($av); | |
} | |
public function getDefault() | |
{ | |
// Default Avatar: http://www.minecraft.net/skin/char.png | |
$output = 'R0lGODlhMAAQAPUuALV7Z6p9ZkUiDkEhDIpMPSgcC2pAMFI9ibSEbZxpTP///7uJciodDTMkEYNVO7eCcpZfQJBeQ5xjRkIdCsaWgL2OdL' | |
. '6IbL2OcqJqRyweDj8qFXpOMy8fDyQYCC8gDUIqEiYaCraJbL2Lco9ePoBTNG1DKpxyXK2AbbN7Yqx2WjQlEoFTOW9FLCseDQAAAAAAAAA' | |
. 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C1hNUCBEYXRhWE1QRD94cDIzRThDRkQwQzcyIiB4' | |
. 'bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkU2RTVBQzAwMDFwYWNrZXQgZW5kPSJyIj8+ACH5BAUAAC4ALAAAAAAwABAAQAZkQJdwSCwaj' | |
. '8ik0uVpcQodUIuxrFqv2OwRoTgAFgdFQEsum8/ocit0oYgqKVVaG4EMCATBaDXv+/+AgYKDVS2GDR8aGQWESAEIAScmCwkJjUcSKA8GBh' | |
. 'YYJJdGLCUDEwICDhuEQQA7'; | |
return base64_decode($output); | |
} | |
public function getInstance($value='') | |
{ | |
return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Mcserver\Utility\MinecraftAvatar'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment