r1106 importing RackTables trunk
[racktables] / render_rack_thumb.php
CommitLineData
e673ee24
DO
1<?
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{
18 global $image;
19 $img = imagecreatefrompng ($image['error']['path']);
20 header("Content-type: image/png");
21 imagepng ($img);
22 imagedestroy ($img);
23}
24
25function colorFromHex ($image, $hex)
26{
27 $r = hexdec ('0x' . substr ($hex, 0, 2));
28 $g = hexdec ('0x' . substr ($hex, 2, 2));
29 $b = hexdec ('0x' . substr ($hex, 4, 2));
30 return imagecolorallocate ($image, $r, $g, $b);
31}
32
33function renderRackThumb ($rack_id = 0)
34{
35 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
36 {
37 renderError();
38 return;
39 }
40 markupObjectProblems ($rackData);
41 global $rtwidth;
42 $offset[0] = 3;
43 $offset[1] = 3 + $rtwidth[0];
44 $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
45 $totalheight = 3 + 3 + $rackData['height'] * 2;
46 $totalwidth = $offset[2] + $rtwidth[2] + 3;
47 $img = @imagecreatetruecolor ($totalwidth, $totalheight);
48 global $color;
49 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, colorFromHex ($img, '000000'));
50 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, colorFromHex ($img, 'c0c0c0'));
51 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, colorFromHex ($img, '000000'));
52 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
53 {
54 for ($locidx = 0; $locidx < 3; $locidx++)
55 {
56 $colorcode = $rackData[$unit_no][$locidx]['state'];
57 if (isset ($rackData[$unit_no][$locidx]['hl']))
58 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
59 imagerectangle
60 (
61 $img,
62 $offset[$locidx],
63 3 + ($rackData['height'] - $unit_no) * 2,
64 $offset[$locidx] + $rtwidth[$locidx] - 1,
65 3 + ($rackData['height'] - $unit_no) * 2 + 1,
66 colorFromHex ($img, $color[$colorcode])
67 );
68 }
69 }
70 header("Content-type: image/png");
71 imagepng ($img);
72 imagedestroy ($img);
73}
74
75?>