r1186 + handle errors better
[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{
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
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;
aa0e4c55
DO
47 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
48 or die("Cannot Initialize new GD image stream");
e673ee24
DO
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?>