r2745 - stuffInRackspace(): sayonara, this data is in rack structure already
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
e673ee24
DO
3require 'inc/init.php';
4
37e59768 5assertStringArg ('img', __FILE__);
d15b2330
DO
6switch ($_REQUEST['img'])
7{
da958e52 8 case 'minirack': // rack security context
37e59768 9 assertUIntArg ('rack_id', __FILE__);
da958e52 10 $pageno = 'rack';
d15b2330 11 $tabno = 'default';
da958e52
DO
12 fixContext();
13 if (!permitted())
14 renderAccessDeniedImage();
15 else
16 renderRackThumb ($_REQUEST['rack_id']);
d15b2330 17 break;
da958e52 18 case 'progressbar': // no security context
37e59768 19 assertUIntArg ('done', __FILE__, TRUE);
d15b2330
DO
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
37e59768
DO
22 case 'view': // file security context
23 case 'preview':
24 assertUIntArg ('file_id', __FILE__);
25 $pageno = 'file';
26 $tabno = 'default';
27 fixContext();
28 if (!permitted())
29 renderAccessDeniedImage();
30 else
31 renderFilePreview ($_REQUEST['file_id']);
32 break;
d15b2330
DO
33 default:
34 renderError();
35}
e673ee24
DO
36
37//------------------------------------------------------------------------
38function renderError ()
39{
738363f6
DO
40 // A hardcoded value is worth of saving lots of code here.
41 $img = imagecreatefrompng ('pix/error.png');
e673ee24 42 header("Content-type: image/png");
da958e52
DO
43 imagepng ($img);
44 imagedestroy ($img);
45}
46
9c0b0016 47// Having a local caching array speeds things up. A little.
e673ee24
DO
48function colorFromHex ($image, $hex)
49{
9c0b0016
DO
50 static $colorcache = array ();
51 if (isset ($colorcache[$hex]))
52 return $colorcache[$hex];
e673ee24
DO
53 $r = hexdec ('0x' . substr ($hex, 0, 2));
54 $g = hexdec ('0x' . substr ($hex, 2, 2));
55 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
56 $c = imagecolorallocate ($image, $r, $g, $b);
57 $colorcache[$hex] = $c;
58 return $c;
e673ee24
DO
59}
60
61function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
62{
63 // Don't call DB extra times, hence we are most probably not the
64 // only script wishing to acces the same data now.
61a1d996
DO
65 if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
66 {
67 header("Content-type: image/png");
35ee7bd0 68 echo $thumbcache;
61a1d996
DO
69 return;
70 }
71 ob_start();
72 if (FALSE !== generateMiniRack ($rack_id))
35ee7bd0 73 {
61a1d996
DO
74 $capture = ob_get_clean();
75 header("Content-type: image/png");
76 echo $capture;
35ee7bd0 77 saveThumbCache ($rack_id, $capture);
61a1d996 78 return;
35ee7bd0 79 }
61a1d996
DO
80 // error text in the buffer
81 ob_end_flush();
35ee7bd0
DO
82}
83
61a1d996
DO
84// Output a binary string containing the PNG minirack. Indicate error with return code.
85function generateMiniRack ($rack_id)
e673ee24 86{
61a1d996
DO
87 if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
88 return FALSE;
89 amplifyCell ($rackData);
e673ee24 90 markupObjectProblems ($rackData);
529eac25 91 global $rtwidth;
d5157018 92 $rtdepth = 9;
e673ee24
DO
93 $offset[0] = 3;
94 $offset[1] = 3 + $rtwidth[0];
95 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
96 $totalheight = 3 + 3 + $rackData['height'] * 2;
97 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
98 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
99 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
100 // cache our palette as well
101 $color = array();
102 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 103 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
104 $color['black'] = colorFromHex ($img, '000000');
105 $color['gray'] = colorFromHex ($img, 'c0c0c0');
106 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
107 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
108 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
109 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
110 {
111 for ($locidx = 0; $locidx < 3; $locidx++)
112 {
113 $colorcode = $rackData[$unit_no][$locidx]['state'];
114 if (isset ($rackData[$unit_no][$locidx]['hl']))
115 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
116 imagerectangle
117 (
118 $img,
119 $offset[$locidx],
120 3 + ($rackData['height'] - $unit_no) * 2,
121 $offset[$locidx] + $rtwidth[$locidx] - 1,
122 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 123 $color[$colorcode]
e673ee24
DO
124 );
125 }
126 }
e673ee24
DO
127 imagepng ($img);
128 imagedestroy ($img);
61a1d996 129 return TRUE;
e673ee24
DO
130}
131
d15b2330
DO
132function renderProgressBarImage ($done)
133{
d1adb55a 134 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
135 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
136 {
137 case 'sparenetwork':
138 $color['T'] = colorFromHex ($img, '808080');
139 $color['F'] = colorFromHex ($img, 'c0c0c0');
140 break;
141 case 'rackspace': // teal
142 default:
143 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
144 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
145 }
d1adb55a
DO
146 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
147 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
d15b2330
DO
148 header("Content-type: image/png");
149 imagepng ($img);
150 imagedestroy ($img);
d15b2330
DO
151}
152
2d75c30b
DO
153function renderAccessDeniedImage ()
154{
155 $img = @imagecreatetruecolor (1, 1);
156 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
157 header("Content-type: image/png");
158 imagepng ($img);
159 imagedestroy ($img);
160}
161
37e59768
DO
162function renderFilePreview ($file_id = 0)
163{
164 $file = getFile ($file_id);
165 $image = imagecreatefromstring ($file['contents']);
166 $width = imagesx ($image);
167 $height = imagesy ($image);
168 if ($_REQUEST['img'] == 'preview' and ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS')))
169 {
170 // TODO: cache thumbs for faster page generation
171 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
172 $newwidth = $width * $ratio;
173 $newheight = $height * $ratio;
174 $resampled = imagecreatetruecolor ($newwidth, $newheight);
175 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
176 imagedestroy ($image);
177 $image = $resampled;
178 unset ($resampled);
179 }
180 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
181 imagepng ($image);
182 imagedestroy ($image);
183}
184
e673ee24 185?>