Skip to content

Instantly share code, notes, and snippets.

@applenick
Forked from vemacs/Skin.php
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save applenick/d3217a67bbe83e15073f to your computer and use it in GitHub Desktop.

Select an option

Save applenick/d3217a67bbe83e15073f to your computer and use it in GitHub Desktop.
<?php
include 'WideImage/WideImage.php';
include 'Skin.php';
# setup uri
$uri = explode('/', trim($_GET['q'], '/'));
# setup variables
$username = $uri[0] == null ? 'char' : $uri[0];
$size = @str_replace(".png", "", ($uri[1] == null ? '32' : $uri[1]));
# get skin
$img = Skin::get($uri[0]);
# has helmet?
$bg = $img->getColorAt(0,0);
$hasHelmet = false;
for($i = 1; $i <= 8; $i++) {
for ($j = 1; $j <= 4; $j++) {
if($img->getColorAt(40 + $i, 7 + $j) != $bg) {
$hasHelmet = true;
break;
}
}
}
# setup helm if has helmet
if($hasHelmet) {
$helm = $img->crop(40, 8, 8, 8);
}
# crop image to just head
$head = $img->crop(8, 8, 8, 8);
# merge head and helm if has helm
if ($hasHelmet) {
$head = $head->merge($helm);
}
# resize and show the result
$final = $head->resize($size);
$final->output(".png");
?>
<?php
class Skin {
public static function get($username) {
$contents = self::fetch('http://skins.minecraft.net/MinecraftSkins/' . $username . '.png');
$default = WideImage::load("char.png");
if ($contents === false) {
header("Status: 404 Not Found");
return $default;
} else {
try {
$img = WideImage::load($contents);
} catch (Exception $e) {
return $default;
}
return $img;
}
}
private static function fetch($url) {
$handle = curl_init($url);
if (false === $handle) {
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Java/1.7.0_51")); // request as if MC client
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($handle);
curl_close($handle);
return $contents;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment