r4353 renderAccessDeniedImage(): store image inline instead of generating
[racktables] / wwwroot / inc / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
4afb4c10 3function dispatchImageRequest()
d15b2330 4{
4afb4c10
DO
5 genericAssertion ('img', 'string');
6 global $pageno, $tabno;
7 switch ($_REQUEST['img'])
8 {
da958e52 9 case 'minirack': // rack security context
da958e52 10 $pageno = 'rack';
d15b2330 11 $tabno = 'default';
da958e52
DO
12 fixContext();
13 if (!permitted())
14 renderAccessDeniedImage();
15 else
4afb4c10 16 renderRackThumb (getBypassValue());
d15b2330 17 break;
da958e52 18 case 'progressbar': // no security context
0cc24e9a 19 assertUIntArg ('done', TRUE);
21d40daa 20 // 'progressbar's never change, make browser cache the result
d15b2330
DO
21 renderProgressBarImage ($_REQUEST['done']);
22 break;
0df8c52b 23 case 'preview': // file security context
37e59768 24 $pageno = 'file';
0df8c52b 25 $tabno = 'download';
37e59768
DO
26 fixContext();
27 if (!permitted())
28 renderAccessDeniedImage();
29 else
4afb4c10 30 renderFilePreview (getBypassValue());
37e59768 31 break;
d15b2330 32 default:
11566bd6 33 renderErrorImage();
4afb4c10 34 }
90a3d6d8
DY
35}
36
11566bd6 37function renderErrorImage ()
e673ee24 38{
e673ee24 39 header("Content-type: image/png");
d5f8fa19
DO
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 );
da958e52
DO
49}
50
9077fd0e
DO
51function 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
9c0b0016 62// Having a local caching array speeds things up. A little.
e673ee24
DO
63function colorFromHex ($image, $hex)
64{
9c0b0016
DO
65 static $colorcache = array ();
66 if (isset ($colorcache[$hex]))
67 return $colorcache[$hex];
e673ee24
DO
68 $r = hexdec ('0x' . substr ($hex, 0, 2));
69 $g = hexdec ('0x' . substr ($hex, 2, 2));
70 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
71 $c = imagecolorallocate ($image, $r, $g, $b);
72 $colorcache[$hex] = $c;
73 return $c;
e673ee24
DO
74}
75
76function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
77{
78 // Don't call DB extra times, hence we are most probably not the
6b06a019 79 // only script wishing to access the same data now.
61a1d996
DO
80 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
81 {
82 header("Content-type: image/png");
35ee7bd0 83 echo $thumbcache;
61a1d996
DO
84 return;
85 }
86 ob_start();
87 if (FALSE !== generateMiniRack ($rack_id))
35ee7bd0 88 {
61a1d996
DO
89 $capture = ob_get_clean();
90 header("Content-type: image/png");
91 echo $capture;
68a0bd8a 92 usePreparedUpdateBlade ('Rack', array ('thumb_data' => base64_encode ($capture)), array ('id' => $rack_id));
61a1d996 93 return;
35ee7bd0 94 }
61a1d996
DO
95 // error text in the buffer
96 ob_end_flush();
35ee7bd0
DO
97}
98
61a1d996
DO
99// Output a binary string containing the PNG minirack. Indicate error with return code.
100function generateMiniRack ($rack_id)
e673ee24 101{
61a1d996
DO
102 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
103 return FALSE;
104 amplifyCell ($rackData);
e673ee24 105 markupObjectProblems ($rackData);
529eac25 106 global $rtwidth;
d5157018 107 $rtdepth = 9;
e673ee24
DO
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;
aa0e4c55
DO
113 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
114 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
115 // cache our palette as well
116 $color = array();
117 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 118 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
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++)
e673ee24
DO
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,
d1adb55a 138 $color[$colorcode]
e673ee24
DO
139 );
140 }
141 }
e673ee24
DO
142 imagepng ($img);
143 imagedestroy ($img);
61a1d996 144 return TRUE;
e673ee24
DO
145}
146
d15b2330
DO
147function renderProgressBarImage ($done)
148{
d1adb55a 149 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
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 }
d1adb55a
DO
161 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
162 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
074106cf
DO
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 }
d15b2330
DO
173 header("Content-type: image/png");
174 imagepng ($img);
175 imagedestroy ($img);
d15b2330
DO
176}
177
4afb4c10 178function renderFilePreview ($file_id)
37e59768 179{
4afb4c10 180 if ($image = getFileCache ($file_id)) //Cache Hit
37e59768 181 {
4afb4c10
DO
182 header("Content-type: image/jpeg");
183 echo $image;
184 return;
37e59768 185 }
4afb4c10
DO
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());
37e59768 208}
f537da2f 209
e673ee24 210?>