r2320 added ability to delete objects
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24 2
e673ee24
DO
3require 'inc/init.php';
4
da958e52 5assertStringArg ('img', 'render_image');
d15b2330
DO
6switch ($_REQUEST['img'])
7{
da958e52
DO
8 case 'minirack': // rack security context
9 assertUIntArg ('rack_id', 'render_image');
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
DO
18 case 'progressbar': // no security context
19 assertUIntArg ('done', 'render_image', TRUE);
d15b2330
DO
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
22 default:
23 renderError();
24}
e673ee24
DO
25
26//------------------------------------------------------------------------
27function renderError ()
28{
738363f6
DO
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
e673ee24 31 header("Content-type: image/png");
da958e52
DO
32 imagepng ($img);
33 imagedestroy ($img);
34}
35
9c0b0016 36// Having a local caching array speeds things up. A little.
e673ee24
DO
37function colorFromHex ($image, $hex)
38{
9c0b0016
DO
39 static $colorcache = array ();
40 if (isset ($colorcache[$hex]))
41 return $colorcache[$hex];
e673ee24
DO
42 $r = hexdec ('0x' . substr ($hex, 0, 2));
43 $g = hexdec ('0x' . substr ($hex, 2, 2));
44 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
45 $c = imagecolorallocate ($image, $r, $g, $b);
46 $colorcache[$hex] = $c;
47 return $c;
e673ee24
DO
48}
49
50function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
51{
52 // Don't call DB extra times, hence we are most probably not the
53 // only script wishing to acces the same data now.
54 header("Content-type: image/png");
55 $thumbcache = loadThumbCache ($rack_id);
56 if ($thumbcache !== NULL)
57 echo $thumbcache;
58 else
59 {
60 ob_start();
61 generateMiniRack ($rack_id);
62 $capture = ob_get_contents();
63 ob_end_flush();
64 saveThumbCache ($rack_id, $capture);
65 }
66}
67
6874300e 68// Output a binary string containing the PNG minirack.
d5157018 69function generateMiniRack ($rack_id = 0)
e673ee24
DO
70{
71 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
72 {
73 renderError();
74 return;
75 }
76 markupObjectProblems ($rackData);
9c0b0016
DO
77 // Cache in a local array, because we are going to use those values often.
78 $rtwidth = array
79 (
80 0 => getConfigVar ('rtwidth_0'),
81 1 => getConfigVar ('rtwidth_1'),
82 2 => getConfigVar ('rtwidth_2')
83 );
d5157018 84 $rtdepth = 9;
e673ee24
DO
85 $offset[0] = 3;
86 $offset[1] = 3 + $rtwidth[0];
87 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
88 $totalheight = 3 + 3 + $rackData['height'] * 2;
89 $totalwidth = $offset[2] + $rtwidth[2] + 3;
aa0e4c55
DO
90 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
91 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
92 // cache our palette as well
93 $color = array();
94 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 95 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
d5157018
DO
96 $color['black'] = colorFromHex ($img, '000000');
97 $color['gray'] = colorFromHex ($img, 'c0c0c0');
98 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
99 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
100 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
101 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
e673ee24
DO
102 {
103 for ($locidx = 0; $locidx < 3; $locidx++)
104 {
105 $colorcode = $rackData[$unit_no][$locidx]['state'];
106 if (isset ($rackData[$unit_no][$locidx]['hl']))
107 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
108 imagerectangle
109 (
110 $img,
111 $offset[$locidx],
112 3 + ($rackData['height'] - $unit_no) * 2,
113 $offset[$locidx] + $rtwidth[$locidx] - 1,
114 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 115 $color[$colorcode]
e673ee24
DO
116 );
117 }
118 }
e673ee24
DO
119 imagepng ($img);
120 imagedestroy ($img);
121}
122
d15b2330
DO
123function renderProgressBarImage ($done)
124{
d1adb55a 125 $img = @imagecreatetruecolor (100, 10);
fec0c8da
DO
126 switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
127 {
128 case 'sparenetwork':
129 $color['T'] = colorFromHex ($img, '808080');
130 $color['F'] = colorFromHex ($img, 'c0c0c0');
131 break;
132 case 'rackspace': // teal
133 default:
134 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
135 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
136 }
d1adb55a
DO
137 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
138 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
d15b2330
DO
139 header("Content-type: image/png");
140 imagepng ($img);
141 imagedestroy ($img);
d15b2330
DO
142}
143
2d75c30b
DO
144function renderAccessDeniedImage ()
145{
146 $img = @imagecreatetruecolor (1, 1);
147 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
148 header("Content-type: image/png");
149 imagepng ($img);
150 imagedestroy ($img);
151}
152
e673ee24 153?>