Skip to content

Instantly share code, notes, and snippets.

@MineTheCube
Created September 1, 2016 18:27
Show Gist options
  • Save MineTheCube/e4c33486d8a3183f50f98fd64780f9a8 to your computer and use it in GitHub Desktop.
Save MineTheCube/e4c33486d8a3183f50f98fd64780f9a8 to your computer and use it in GitHub Desktop.
Get head picture from base64 encoded texture
<?php
// Encoded equivalent of:
// {"textures":{"SKIN":{"url":"http://textures.minecraft.net/texture/56936d4f0d1b93fef775b1fbd19281b70c6f88475bb5a41bf372c12f1f8a22"}}}
$base64 = 'eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTY5MzZkNGYwZDFiOTNmZWY3NzViMWZiZDE5MjgxYjcwYzZmODg0NzViYjVhNDFiZjM3MmMxMmYxZjhhMjIifX19';
// Return skin from base64 texture
function get_head_from_texture($base64) {
$texture = json_decode(base64_decode($base64), true);
if (is_array($texture) and isset($texture['textures'], $texture['textures']['SKIN'], $texture['textures']['SKIN']['url'])) {
return file_get_contents($texture['textures']['SKIN']['url']);
} else {
return null;
}
}
// Require MojangAPI
// Get it here: https://github.com/MineTheCube/MojangAPI/blob/master/mojang-api.class.php
require 'mojang-api.class.php';
// Get skin
$skin = get_head_from_texture($base64);
if ($skin) {
// Get head from skin
$head = MojangAPI::getPlayerHeadFromSkin($skin, 100);
if ($head) {
// Display it to browser
echo '<img src="' . MojangAPI::embedImage($head) . '" alt="Skin head">';
} else {
echo 'Cannot get head from skin.';
}
} else {
echo 'Cannot get skin from texture.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment