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