3 // This page outputs PNG rack thumbnail.
5 require 'inc/init.php';
7 // Thumbnails are rendered in security context of rackspace.
12 assertUIntArg ('rack_id');
13 renderRackThumb ($_REQUEST['rack_id']);
15 //------------------------------------------------------------------------
16 function renderError ()
18 // A hardcoded value is worth of saving lots of code here.
19 $img = imagecreatefrompng ('pix/error.png');
20 header("Content-type: image/png");
25 // Having a local caching array speeds things up. A little.
26 function colorFromHex ($image, $hex)
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;
39 function renderRackThumb ($rack_id = 0)
41 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
46 markupObjectProblems ($rackData);
47 // Cache in a local array, because we are going to use those values often.
50 0 => getConfigVar ('rtwidth_0'),
51 1 => getConfigVar ('rtwidth_1'),
52 2 => getConfigVar ('rtwidth_2')
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
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--)
70 for ($locidx = 0; $locidx < 3; $locidx++
)
72 $colorcode = $rackData[$unit_no][$locidx]['state'];
73 if (isset ($rackData[$unit_no][$locidx]['hl']))
74 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
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])
86 header("Content-type: image/png");