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