r1500 + render_rack_thumb.php becomes render_image.php
[racktables] / render_image.php
1 <?php
2
3 // This page outputs PNG rack thumbnail.
4
5 require 'inc/init.php';
6
7 assertStringArg ('img');
8 switch ($_REQUEST['img'])
9 {
10 case 'minirack':
11 // Thumbnails are rendered in security context of rackspace.
12 $pageno = 'rackspace';
13 $tabno = 'default';
14 authorize();
15 assertUIntArg ('rack_id');
16 renderRackThumb ($_REQUEST['rack_id']);
17 break;
18 case 'progressbar':
19 assertUIntArg ('done', TRUE);
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
22 default:
23 renderError();
24 }
25
26 //------------------------------------------------------------------------
27 function renderError ()
28 {
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
31 header("Content-type: image/png");
32 imagepng ($img);
33 imagedestroy ($img);
34 }
35
36 // Having a local caching array speeds things up. A little.
37 function colorFromHex ($image, $hex)
38 {
39 static $colorcache = array ();
40 if (isset ($colorcache[$hex]))
41 return $colorcache[$hex];
42 $r = hexdec ('0x' . substr ($hex, 0, 2));
43 $g = hexdec ('0x' . substr ($hex, 2, 2));
44 $b = hexdec ('0x' . substr ($hex, 4, 2));
45 $c = imagecolorallocate ($image, $r, $g, $b);
46 $colorcache[$hex] = $c;
47 return $c;
48 }
49
50 function renderRackThumb ($rack_id = 0)
51 {
52 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
53 {
54 renderError();
55 return;
56 }
57 markupObjectProblems ($rackData);
58 // Cache in a local array, because we are going to use those values often.
59 $rtwidth = array
60 (
61 0 => getConfigVar ('rtwidth_0'),
62 1 => getConfigVar ('rtwidth_1'),
63 2 => getConfigVar ('rtwidth_2')
64 );
65 $offset[0] = 3;
66 $offset[1] = 3 + $rtwidth[0];
67 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
68 $totalheight = 3 + 3 + $rackData['height'] * 2;
69 $totalwidth = $offset[2] + $rtwidth[2] + 3;
70 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
71 or die("Cannot Initialize new GD image stream");
72 // cache our palette as well
73 $color = array();
74 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
75 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
76 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, colorFromHex ($img, '000000'));
77 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, colorFromHex ($img, 'c0c0c0'));
78 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, colorFromHex ($img, '000000'));
79 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
80 {
81 for ($locidx = 0; $locidx < 3; $locidx++)
82 {
83 $colorcode = $rackData[$unit_no][$locidx]['state'];
84 if (isset ($rackData[$unit_no][$locidx]['hl']))
85 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
86 imagerectangle
87 (
88 $img,
89 $offset[$locidx],
90 3 + ($rackData['height'] - $unit_no) * 2,
91 $offset[$locidx] + $rtwidth[$locidx] - 1,
92 3 + ($rackData['height'] - $unit_no) * 2 + 1,
93 $color[$colorcode]
94 );
95 }
96 }
97 header("Content-type: image/png");
98 imagepng ($img);
99 imagedestroy ($img);
100 }
101
102 function renderProgressBarImage ($done)
103 {
104 $img = @imagecreatetruecolor (100, 10);
105 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
106 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
107 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
108 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
109 header("Content-type: image/png");
110 imagepng ($img);
111 imagedestroy ($img);
112 }
113
114 ?>