1 | <?php |
---|
2 | |
---|
3 | class thumb |
---|
4 | { |
---|
5 | function thumb($str_id, $size, $type = 'pilot') |
---|
6 | { |
---|
7 | $this->_id = $str_id; |
---|
8 | $this->_size = $size; |
---|
9 | $this->_type = $type; |
---|
10 | $this->_encoding = 'jpeg'; |
---|
11 | |
---|
12 | $this->validate(); |
---|
13 | } |
---|
14 | |
---|
15 | function display() |
---|
16 | { |
---|
17 | if (!$this->isCached()) |
---|
18 | { |
---|
19 | if (!$this->genCache()) |
---|
20 | { |
---|
21 | return false; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | if (headers_sent()) |
---|
26 | { |
---|
27 | echo 'Error occured.<br/>'; |
---|
28 | return false; |
---|
29 | } |
---|
30 | if ($this->_encoding == 'jpeg') |
---|
31 | { |
---|
32 | header("Content-Type: image/jpeg"); |
---|
33 | readfile($this->_thumb); |
---|
34 | } |
---|
35 | elseif ($this->_encoding == 'png') |
---|
36 | { |
---|
37 | header("Content-Type: image/png"); |
---|
38 | readfile($this->_thumb); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | function validate() |
---|
43 | { |
---|
44 | if (!$this->_size) |
---|
45 | { |
---|
46 | $this->_size = 32; |
---|
47 | } |
---|
48 | switch ($this->_type) |
---|
49 | { |
---|
50 | case 'corp': |
---|
51 | $this->_id = intval($this->_id); |
---|
52 | break; |
---|
53 | case 'alliance': |
---|
54 | $this->_id = preg_replace('/[^a-zA-Z0-9]/', '', $this->_id); |
---|
55 | if (!strlen($this->_id)) |
---|
56 | { |
---|
57 | $this->_id = 'default'; |
---|
58 | } |
---|
59 | break; |
---|
60 | default: |
---|
61 | $this->_type = 'pilot'; |
---|
62 | $this->_id = intval($this->_id); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | function isCached() |
---|
67 | { |
---|
68 | switch ($this->_type) |
---|
69 | { |
---|
70 | case 'pilot': |
---|
71 | $this->_thumb = 'cache/portraits/'.$this->_id.'_'.$this->_size.'.jpg'; |
---|
72 | break; |
---|
73 | case 'corp': |
---|
74 | $this->_thumb = 'cache/corps/'.$this->_id.'_'.$this->_size.'.jpg'; |
---|
75 | break; |
---|
76 | case 'alliance': |
---|
77 | $this->_thumb = 'cache/corps/all'.$this->_id.'_'.$this->_size.'.png'; |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | if (file_exists($this->_thumb)) |
---|
82 | { |
---|
83 | return true; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | function genCache() |
---|
88 | { |
---|
89 | switch ($this->_type) |
---|
90 | { |
---|
91 | case 'pilot': |
---|
92 | $this->genPilot(); |
---|
93 | break; |
---|
94 | case 'corp': |
---|
95 | $this->genCorp(); |
---|
96 | break; |
---|
97 | case 'alliance': |
---|
98 | $this->genAlliance(); |
---|
99 | break; |
---|
100 | } |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | function genPilot() |
---|
105 | { |
---|
106 | if (file_exists('cache/portraits/'.$this->_id.'_256.jpg')) |
---|
107 | { |
---|
108 | $img = imagecreatefromjpeg('cache/portraits/'.$this->_id.'_256.jpg'); |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | if ($this->_id) |
---|
113 | { |
---|
114 | // check for a valid, known external id |
---|
115 | $qry = new DBQuery(); |
---|
116 | $qry->execute('SELECT plt_externalid FROM kb3_pilots WHERE plt_externalid = '.$this->_id.' LIMIT 1'); |
---|
117 | $row = $qry->getRow(); |
---|
118 | if (!$id = $row['plt_externalid']) |
---|
119 | { |
---|
120 | // there is no such id so set it to 0 |
---|
121 | $this->_id = 0; |
---|
122 | $this->_thumb = 'img/portrait_0_'.$this->_size.'.jpg'; |
---|
123 | return; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | @ini_set('default_socket_timeout', 5); |
---|
128 | $file = @file_get_contents('http://img.eve.is/serv.asp?s=256&c='.$this->_id); |
---|
129 | if ($img = @imagecreatefromstring($file)) |
---|
130 | { |
---|
131 | $fp = fopen('cache/portraits/'.$this->_id.'_256.jpg', 'w'); |
---|
132 | fwrite($fp, $file); |
---|
133 | fclose($fp); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | // try alternative access via fsockopen |
---|
138 | // happens if allow_url_fopen wrapper is false |
---|
139 | require_once('class.http.php'); |
---|
140 | |
---|
141 | $url = 'http://img.eve.is/serv.asp?s=256&c='.$this->_id; |
---|
142 | $http = new http_request($url); |
---|
143 | $file = $http->get_content(); |
---|
144 | |
---|
145 | if ($img = @imagecreatefromstring($file)) |
---|
146 | { |
---|
147 | $fp = fopen('cache/portraits/'.$id.'_256.jpg', 'w'); |
---|
148 | fwrite($fp, $file); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | if ($img) |
---|
154 | { |
---|
155 | $newimg = imagecreatetruecolor($this->_size, $this->_size); |
---|
156 | imagecopyresampled($newimg, $img, 0, 0, 0, 0, $this->_size, $this->_size, 256, 256); |
---|
157 | imagejpeg($newimg, $this->_thumb, 90); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | // fallback to a portrait with red ! |
---|
162 | $this->_thumb = 'img/portrait_0_'.$this->_size.'.jpg'; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | function genCorp() |
---|
167 | { |
---|
168 | if (!file_exists('img/corps/'.$this->_id.'.jpg')) |
---|
169 | { |
---|
170 | $this->_id = 0; |
---|
171 | } |
---|
172 | $img = imagecreatefromjpeg('img/corps/'.$this->_id.'.jpg'); |
---|
173 | if ($img) |
---|
174 | { |
---|
175 | $newimg = imagecreatetruecolor($this->_size, $this->_size); |
---|
176 | $oldx = imagesx($img); |
---|
177 | $oldy = imagesy($img); |
---|
178 | imagecopyresampled($newimg, $img, 0, 0, 0, 0, $this->_size, $this->_size, $oldx, $oldy); |
---|
179 | imagejpeg($newimg, $this->_thumb, 90); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | function genAlliance() |
---|
184 | { |
---|
185 | if (!file_exists('img/alliances/'.$this->_id.'.png')) |
---|
186 | { |
---|
187 | $this->_id = 0; |
---|
188 | } |
---|
189 | $img = imagecreatefromjpeg('img/alliances/'.$this->_id.'.png'); |
---|
190 | if ($img) |
---|
191 | { |
---|
192 | $newimg = imagecreatetruecolor($this->_size, $this->_size); |
---|
193 | $oldx = imagesx($img); |
---|
194 | $oldy = imagesy($img); |
---|
195 | imagecopyresampled($newimg, $img, 0, 0, 0, 0, $this->_size, $this->_size, $oldx, $oldy); |
---|
196 | imagepng($newimg, $this->_thumb); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | ?> |
---|