r1485 + adding a real server to a pool is possible now
[racktables] / render_rack_thumb.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2
3// This page outputs PNG rack thumbnail.
4
5require 'inc/init.php';
6
7// Thumbnails are rendered in security context of rackspace.
8$pageno = 'rackspace';
9$tabno = 'default';
10authorize();
11
12assertUIntArg ('rack_id');
13renderRackThumb ($_REQUEST['rack_id']);
14
15//------------------------------------------------------------------------
16function renderError ()
17{
738363f6
DO
18 // A hardcoded value is worth of saving lots of code here.
19 $img = imagecreatefrompng ('pix/error.png');
e673ee24
DO
20 header("Content-type: image/png");
21 imagepng ($img);
22 imagedestroy ($img);
23}
24
9c0b0016 25// Having a local caching array speeds things up. A little.
e673ee24
DO
26function colorFromHex ($image, $hex)
27{
9c0b0016
DO
28 static $colorcache = array ();
29 if (isset ($colorcache[$hex]))
30 return $colorcache[$hex];
e673ee24
DO
31 $r = hexdec ('0x' . substr ($hex, 0, 2));
32 $g = hexdec ('0x' . substr ($hex, 2, 2));
33 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
34 $c = imagecolorallocate ($image, $r, $g, $b);
35 $colorcache[$hex] = $c;
36 return $c;
e673ee24
DO
37}
38
39function renderRackThumb ($rack_id = 0)
40{
41 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
42 {
43 renderError();
44 return;
45 }
46 markupObjectProblems ($rackData);
9c0b0016
DO
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 );
e673ee24
DO
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;
aa0e4c55
DO
59 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
60 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
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);
e673ee24
DO
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?>