Skip to content

Instantly share code, notes, and snippets.

@suinua
Last active May 23, 2021 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suinua/315d8239dce060615e184acf2264bbfe to your computer and use it in GitHub Desktop.
Save suinua/315d8239dce060615e184acf2264bbfe to your computer and use it in GitHub Desktop.
プレイヤーのスキンをpngファイルで保存します
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