3 // This page outputs PNG rack thumbnail.
5 require 'inc/init.php';
7 assertStringArg ('img', __FUNCTION__
);
8 switch ($_REQUEST['img'])
11 // Thumbnails are rendered in security context of rackspace.
12 $pageno = 'rackspace';
15 assertUIntArg ('rack_id', __FUNCTION__
);
16 renderRackThumb ($_REQUEST['rack_id']);
19 assertUIntArg ('done', __FUNCTION__
, TRUE);
20 renderProgressBarImage ($_REQUEST['done']);
26 //------------------------------------------------------------------------
27 function renderError ()
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
31 header("Content-type: image/png");
36 // Having a local caching array speeds things up. A little.
37 function colorFromHex ($image, $hex)
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;
50 function renderRackThumb ($rack_id = 0)
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)
61 generateMiniRack ($rack_id);
62 $capture = ob_get_contents();
64 saveThumbCache ($rack_id, $capture);
68 // Output a binary string containing the PNG minirack.
69 function generateMiniRack ($rack_id)
71 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
76 markupObjectProblems ($rackData);
77 // Cache in a local array, because we are going to use those values often.
80 0 => getConfigVar ('rtwidth_0'),
81 1 => getConfigVar ('rtwidth_1'),
82 2 => getConfigVar ('rtwidth_2')
85 $offset[1] = 3 +
$rtwidth[0];
86 $offset[2] = 3 +
$rtwidth[0] +
$rtwidth[1];
87 $totalheight = 3 +
3 +
$rackData['height'] * 2;
88 $totalwidth = $offset[2] +
$rtwidth[2] +
3;
89 $img = @imagecreatetruecolor
($totalwidth, $totalheight)
90 or die("Cannot Initialize new GD image stream");
91 // cache our palette as well
93 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
94 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
95 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, colorFromHex ($img, '000000'));
96 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, colorFromHex ($img, 'c0c0c0'));
97 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, colorFromHex ($img, '000000'));
98 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
100 for ($locidx = 0; $locidx < 3; $locidx++
)
102 $colorcode = $rackData[$unit_no][$locidx]['state'];
103 if (isset ($rackData[$unit_no][$locidx]['hl']))
104 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
109 3 +
($rackData['height'] - $unit_no) * 2,
110 $offset[$locidx] +
$rtwidth[$locidx] - 1,
111 3 +
($rackData['height'] - $unit_no) * 2 +
1,
120 function renderProgressBarImage ($done)
122 $img = @imagecreatetruecolor
(100, 10);
123 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
124 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
125 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
126 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
127 header("Content-type: image/png");