r1823 + now fix URLs...
[racktables] / render_image.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2
3// This page outputs PNG rack thumbnail.
4
5require 'inc/init.php';
6
d15b2330
DO
7assertStringArg ('img');
8switch ($_REQUEST['img'])
9{
10 case 'minirack':
11 // Thumbnails are rendered in security context of rackspace.
12 $pageno = 'rackspace';
13 $tabno = 'default';
14 authorize();
15 assertUIntArg ('rack_id');
16 renderRackThumb ($_REQUEST['rack_id']);
17 break;
18 case 'progressbar':
c156b264 19 assertUIntArg ('done', TRUE);
d15b2330
DO
20 renderProgressBarImage ($_REQUEST['done']);
21 break;
22 default:
23 renderError();
24}
e673ee24
DO
25
26//------------------------------------------------------------------------
27function renderError ()
28{
738363f6
DO
29 // A hardcoded value is worth of saving lots of code here.
30 $img = imagecreatefrompng ('pix/error.png');
e673ee24
DO
31 header("Content-type: image/png");
32 imagepng ($img);
33 imagedestroy ($img);
34}
35
9c0b0016 36// Having a local caching array speeds things up. A little.
e673ee24
DO
37function colorFromHex ($image, $hex)
38{
9c0b0016
DO
39 static $colorcache = array ();
40 if (isset ($colorcache[$hex]))
41 return $colorcache[$hex];
e673ee24
DO
42 $r = hexdec ('0x' . substr ($hex, 0, 2));
43 $g = hexdec ('0x' . substr ($hex, 2, 2));
44 $b = hexdec ('0x' . substr ($hex, 4, 2));
9c0b0016
DO
45 $c = imagecolorallocate ($image, $r, $g, $b);
46 $colorcache[$hex] = $c;
47 return $c;
e673ee24
DO
48}
49
50function renderRackThumb ($rack_id = 0)
35ee7bd0
DO
51{
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)
57 echo $thumbcache;
58 else
59 {
60 ob_start();
61 generateMiniRack ($rack_id);
62 $capture = ob_get_contents();
63 ob_end_flush();
64 saveThumbCache ($rack_id, $capture);
65 }
66}
67
6874300e 68// Output a binary string containing the PNG minirack.
35ee7bd0 69function generateMiniRack ($rack_id)
e673ee24
DO
70{
71 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
72 {
73 renderError();
74 return;
75 }
76 markupObjectProblems ($rackData);
9c0b0016
DO
77 // Cache in a local array, because we are going to use those values often.
78 $rtwidth = array
79 (
80 0 => getConfigVar ('rtwidth_0'),
81 1 => getConfigVar ('rtwidth_1'),
82 2 => getConfigVar ('rtwidth_2')
83 );
e673ee24
DO
84 $offset[0] = 3;
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;
aa0e4c55
DO
89 $img = @imagecreatetruecolor ($totalwidth, $totalheight)
90 or die("Cannot Initialize new GD image stream");
9c0b0016
DO
91 // cache our palette as well
92 $color = array();
93 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
d1adb55a 94 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
e673ee24
DO
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--)
99 {
100 for ($locidx = 0; $locidx < 3; $locidx++)
101 {
102 $colorcode = $rackData[$unit_no][$locidx]['state'];
103 if (isset ($rackData[$unit_no][$locidx]['hl']))
104 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
105 imagerectangle
106 (
107 $img,
108 $offset[$locidx],
109 3 + ($rackData['height'] - $unit_no) * 2,
110 $offset[$locidx] + $rtwidth[$locidx] - 1,
111 3 + ($rackData['height'] - $unit_no) * 2 + 1,
d1adb55a 112 $color[$colorcode]
e673ee24
DO
113 );
114 }
115 }
e673ee24
DO
116 imagepng ($img);
117 imagedestroy ($img);
118}
119
d15b2330
DO
120function renderProgressBarImage ($done)
121{
d1adb55a
DO
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']);
d15b2330
DO
127 header("Content-type: image/png");
128 imagepng ($img);
129 imagedestroy ($img);
d15b2330
DO
130}
131
e673ee24 132?>