Last active
May 23, 2021 03:39
-
-
Save suinua/315d8239dce060615e184acf2264bbfe to your computer and use it in GitHub Desktop.
プレイヤーのスキンをpngファイルで保存します
This file contains 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
public function onJoin(PlayerJoinEvent $event) { | |
$player = $event->getPlayer(); | |
$skin_raw = $player->getSkin()->getSkinData(); | |
$height = 64; | |
$width = 64; | |
switch (strlen($skin_raw)) { | |
case 64 * 32 * 4: | |
$height = 32; | |
$width = 64; | |
break; | |
case 64 * 64 * 4: | |
$height = 64; | |
$width = 64; | |
break; | |
case 128 * 64 * 4: | |
$height = 64; | |
$width = 128; | |
break; | |
case 128 * 128 * 4: | |
$height = 128; | |
$width = 128; | |
break; | |
} | |
$img = imagecreatetruecolor($width, $height); | |
imagealphablending($img, false); | |
imagesavealpha($img, true); | |
$index = 0; | |
for ($y = 0; $y < $height; ++$y) { | |
for ($x = 0; $x < $width; ++$x) { | |
$list = substr($skin_raw, $index, 4); | |
$r = ord($list[0]); | |
$g = ord($list[1]); | |
$b = ord($list[2]); | |
$a = 127 - (ord($list[3]) >> 1); | |
$index += 4; | |
$color = imagecolorallocatealpha($img, $r, $g, $b, $a); | |
imagesetpixel($img, $x, $y, $color); | |
} | |
} | |
imagepng($img, $this->getDataFolder() . $player->getName() . ".png"); | |
imagedestroy($img); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment