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