r1171 + merged second patch by Aaron
[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 global $image;
19 $img = imagecreatefrompng ($image['error']['path']);
20 header("Content-type: image/png");
21 imagepng ($img);
22 imagedestroy ($img);
23 }
24
25 function 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
33 function 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 or die("Cannot Initialize new GD image stream");
49 global $color;
50 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, colorFromHex ($img, '000000'));
51 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, colorFromHex ($img, 'c0c0c0'));
52 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, colorFromHex ($img, '000000'));
53 for ($unit_no = $rackData['height']; $unit_no > 0; $unit_no--)
54 {
55 for ($locidx = 0; $locidx < 3; $locidx++)
56 {
57 $colorcode = $rackData[$unit_no][$locidx]['state'];
58 if (isset ($rackData[$unit_no][$locidx]['hl']))
59 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
60 imagerectangle
61 (
62 $img,
63 $offset[$locidx],
64 3 + ($rackData['height'] - $unit_no) * 2,
65 $offset[$locidx] + $rtwidth[$locidx] - 1,
66 3 + ($rackData['height'] - $unit_no) * 2 + 1,
67 colorFromHex ($img, $color[$colorcode])
68 );
69 }
70 }
71 header("Content-type: image/png");
72 imagepng ($img);
73 imagedestroy ($img);
74 }
75
76 ?>