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