8fe027fcfc9294b1e890b1d6811161c9c310ae9e
[racktables] / wwwroot / inc / render_image.php
1 <?php
2
3 function dispatchImageRequest()
4 {
5 genericAssertion ('img', 'string');
6 global $pageno, $tabno;
7 switch ($_REQUEST['img'])
8 {
9 case 'minirack': // rack security context
10 $pageno = 'rack';
11 $tabno = 'default';
12 fixContext();
13 assertPermission()
14 renderRackThumb (getBypassValue());
15 break;
16 case 'progressbar': // no security context
17 assertUIntArg ('done', TRUE);
18 // 'progressbar's never change, make browser cache the result
19 renderProgressBarImage ($_REQUEST['done']);
20 break;
21 case 'preview': // file security context
22 $pageno = 'file';
23 $tabno = 'download';
24 fixContext();
25 assertPermission();
26 renderFilePreview (getBypassValue());
27 break;
28 default:
29 renderErrorImage();
30 }
31 }
32
33 function renderErrorImage ()
34 {
35 header("Content-type: image/png");
36 // "ERROR", 76x17, red on white
37 echo base64_decode
38 (
39 'iVBORw0KGgoAAAANSUhEUgAAAEwAAAARCAYAAAB3h0oCAAAAAXNSR0IArs4c6QAAALBJREFUWMPt' .
40 'WFsOwCAIG4v3vzL7WEyWxQdVwM1A4l/F2iHVETPzESGOMyTAInURRP0suUhb2FIho/jWXO38w4KN' .
41 'LPDGEt2jlgPBZxFKc2o8UT7Lj6SkAmfw1nx+28MkVWQlcjT9EOwjLqnpaNImi+I1j/YSl5RY/gx+' .
42 'VCCF/MnkCz4JZQtvEUXx1nyW9jCUlPVLbTJ/3MO2dsnWRq2Nwl2wTarM51rhsVEnDhT/w7C4APaJ' .
43 'ZhkIGYaUAAAAAElFTkSuQmCC'
44 );
45 }
46
47 function renderAccessDeniedImage()
48 {
49 header ('Content-type: image/png');
50 // 1x1, single black pixel
51 echo base64_decode
52 (
53 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdj' .
54 'YGBgAAAABAABJzQnCgAAAABJRU5ErkJggg=='
55 );
56 }
57
58 // Having a local caching array speeds things up. A little.
59 function colorFromHex ($image, $hex)
60 {
61 static $colorcache = array ();
62 if (isset ($colorcache[$hex]))
63 return $colorcache[$hex];
64 $r = hexdec ('0x' . substr ($hex, 0, 2));
65 $g = hexdec ('0x' . substr ($hex, 2, 2));
66 $b = hexdec ('0x' . substr ($hex, 4, 2));
67 $c = imagecolorallocate ($image, $r, $g, $b);
68 $colorcache[$hex] = $c;
69 return $c;
70 }
71
72 function renderRackThumb ($rack_id = 0)
73 {
74 // Don't call DB extra times, hence we are most probably not the
75 // only script wishing to access the same data now.
76 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
77 {
78 header("Content-type: image/png");
79 echo $thumbcache;
80 return;
81 }
82 ob_start();
83 if (FALSE !== generateMiniRack ($rack_id))
84 {
85 $capture = ob_get_clean();
86 header("Content-type: image/png");
87 echo $capture;
88 usePreparedUpdateBlade ('Rack', array ('thumb_data' => base64_encode ($capture)), array ('id' => $rack_id));
89 return;
90 }
91 // error text in the buffer
92 ob_end_flush();
93 }
94
95 // Output a binary string containing the PNG minirack. Indicate error with return code.
96 function generateMiniRack ($rack_id)
97 {
98 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
99 return FALSE;
100 amplifyCell ($rackData);
101 markupObjectProblems ($rackData);
102 global $rtwidth;
103 $rtdepth = 9;
104 $offset[0] = 3;
105 $offset[1] = 3 + $rtwidth[0];
106 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
107 $totalheight = 3 + 3 + $rackData['height'] * 2;
108 $totalwidth = $offset[2] + $rtwidth[2] + 3;
109 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
110 or die("Cannot Initialize new GD image stream");
111 // cache our palette as well
112 $color = array();
113 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
114 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
115 $color['black'] = colorFromHex ($img, '000000');
116 $color['gray'] = colorFromHex ($img, 'c0c0c0');
117 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
118 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
119 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
120 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
121 {
122 for ($locidx = 0; $locidx < 3; $locidx++)
123 {
124 $colorcode = $rackData[$unit_no][$locidx]['state'];
125 if (isset ($rackData[$unit_no][$locidx]['hl']))
126 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
127 imagerectangle
128 (
129 $img,
130 $offset[$locidx],
131 3 + ($rackData['height'] - $unit_no) * 2,
132 $offset[$locidx] + $rtwidth[$locidx] - 1,
133 3 + ($rackData['height'] - $unit_no) * 2 + 1,
134 $color[$colorcode]
135 );
136 }
137 }
138 imagepng ($img);
139 imagedestroy ($img);
140 return TRUE;
141 }
142
143 function renderProgressBarImage ($done)
144 {
145 $img = @imagecreatetruecolor (100, 10);
146 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
147 {
148 case 'sparenetwork':
149 $color['T'] = colorFromHex ($img, '808080');
150 $color['F'] = colorFromHex ($img, 'c0c0c0');
151 break;
152 case 'rackspace': // teal
153 default:
154 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
155 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
156 }
157 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
158 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
159 for ($x = 20; $x <= 80; $x += 20)
160 {
161 $cc = $x > $done ? $color['T'] : $color['F'];
162 imagesetpixel ($img, $x, 0, $cc);
163 imagesetpixel ($img, $x, 1, $cc);
164 imagesetpixel ($img, $x, 4, $cc);
165 imagesetpixel ($img, $x, 5, $cc);
166 imagesetpixel ($img, $x, 8, $cc);
167 imagesetpixel ($img, $x, 9, $cc);
168 }
169 header("Content-type: image/png");
170 imagepng ($img);
171 imagedestroy ($img);
172 }
173
174 function renderFilePreview ($file_id)
175 {
176 if ($image = getFileCache ($file_id)) //Cache Hit
177 {
178 header("Content-type: image/jpeg");
179 echo $image;
180 return;
181 }
182 //Cache Miss
183 $file = getFile ($file_id);
184 $image = imagecreatefromstring ($file['contents']);
185 unset ($file);
186 $width = imagesx ($image);
187 $height = imagesy ($image);
188 if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
189 {
190 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
191 $newwidth = $width * $ratio;
192 $newheight = $height * $ratio;
193 $resampled = imagecreatetruecolor ($newwidth, $newheight);
194 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
195 imagedestroy ($image);
196 $image = $resampled;
197 unset ($resampled);
198 }
199 header ('Content-type: image/jpeg');
200 ob_start();
201 imagejpeg ($image);
202 imagedestroy ($image);
203 commitAddFileCache ($file_id, ob_get_flush());
204 }
205
206 ?>