forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minotar.php
156 lines (141 loc) · 4.68 KB
/
Minotar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
*
*/
class Minotar {
/**
* The Default Skin
*/
const DEFAULT_SKIN = 'char';
/**
* Caching On or Off?
*/
const CACHING = false;
/**
* Cache expiration time
*
* @var int
*/
public static $expires = 86400; // 24 hours in seconds
/**
* Downloads a minecraft skin from Mojang
*
* @param string $username
* @param bool $clear
* @return string
*/
public static function get($username, $clear = false) {
if (!file_exists('./minecraft/skins/' . strtolower($username) . '.png') || $clear) {
$contents = self::fetch('http://s3.amazonaws.com/MinecraftSkins/' . $username . '.png');
if ($contents === false) {
$img = WideImage::load("http://s3.amazonaws.com/MinecraftSkins/char.png");
$img->saveToFile("./minecraft/skins/char.png");
$helm = clone $img;
$helm->crop(40, 8, 8, 8)->saveToFile('./minecraft/helms/char.png');
$head = clone $img;
$head->crop(8, 8, 8, 8)->saveToFile('./minecraft/heads/char.png');
$head->destroy();
$helm->destroy();
$img->destroy();
header("Status: 404 Not Found");
return 'char';
} else {
try {
$img = WideImage::load($contents);
} catch (Exception $e) {
return 'char';
}
$img->saveToFile('./minecraft/skins/' . strtolower($username) . '.png');
$helm = clone $img;
$helm->crop(40, 8, 8, 8)->saveToFile('./minecraft/helms/' . strtolower($username) . '.png');
$head = clone $img;
$head->crop(8, 8, 8, 8)->saveToFile('./minecraft/heads/' . strtolower($username) . '.png');
$head->destroy();
$helm->destroy();
$img->destroy();
return strtolower($username);
}
} else {
return strtolower($username);
}
}
/**
* Gets the files from a directory
*
* @param $dir
* @return array
*/
public static function getFilesFromDir($dir) {
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "Thumbs.db") {
if (is_dir($dir . '/' . $file)) {
$dir2 = $dir . '/' . $file;
$files[] = getFilesFromDir($dir2);
} else {
$files[] = $dir . '/' . $file;
}
}
}
closedir($handle);
}
return Minotar::array_flat($files);
}
/**
* Flattens an array
*
* @param array $array
* @return array
*/
private static function array_flat($array) {
foreach ($array as $a) {
if (is_array($a)) {
$tmp = array();
$tmp = array_merge($tmp, array_flat($a));
} else {
$tmp[] = $a;
}
}
return $tmp;
}
/**
* Checks URL to make sure it exists
*
* @param string $url
* @return bool|mixed
*/
private function found($url) {
$handle = curl_init($url);
if (false === $handle) {
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15")); // request as if Firefox
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$connectable = curl_exec($handle);
curl_close($handle);
return $connectable;
}
/**
* wget for PHP
*
* @param string $url
* @return bool|mixed
*/
private static function fetch($url) {
$handle = curl_init($url);
if (false === $handle) {
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15")); // request as if Firefox
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($handle);
curl_close($handle);
return $contents;
}
}