root/trunk/RackTables/render_image.php

Revision 2222, 4.3 kB (checked in by pilot, 4 months ago)
  • generate "access denied" image from CPU, not disk
  • employ padding instead of spacer image (discard it)
  • switch back from BCMath and revert related release notes additions
Line 
1 <?php
2
3 require 'inc/init.php';
4
5 assertStringArg ('img', 'render_image');
6 switch ($_REQUEST['img'])
7 {
8     case 'minirack': // rack security context
9         assertUIntArg ('rack_id', 'render_image');
10         $pageno = 'rack';
11         $tabno = 'default';
12         fixContext();
13         if (!permitted())
14             renderAccessDeniedImage();
15         else
16             renderRackThumb ($_REQUEST['rack_id']);
17         break;
18     case 'progressbar': // no security context
19         assertUIntArg ('done', 'render_image', TRUE);
20         renderProgressBarImage ($_REQUEST['done']);
21         break;
22     default:
23         renderError();
24 }
25
26 //------------------------------------------------------------------------
27 function renderError ()
28 {
29     // A hardcoded value is worth of saving lots of code here.
30     $img = imagecreatefrompng ('pix/error.png');
31     header("Content-type: image/png");
32     imagepng ($img);
33     imagedestroy ($img);
34 }
35
36 // Having a local caching array speeds things up. A little.
37 function colorFromHex ($image, $hex)
38 {
39     static $colorcache = array ();
40     if (isset ($colorcache[$hex]))
41         return $colorcache[$hex];
42     $r = hexdec ('0x' . substr ($hex, 0, 2));
43     $g = hexdec ('0x' . substr ($hex, 2, 2));
44     $b = hexdec ('0x' . substr ($hex, 4, 2));
45     $c = imagecolorallocate ($image, $r, $g, $b);
46     $colorcache[$hex] = $c;
47     return $c;
48 }
49
50 function renderRackThumb ($rack_id = 0)
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
68 // Output a binary string containing the PNG minirack.
69 function generateMiniRack ($rack_id = 0)
70 {
71     if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
72     {
73         renderError();
74         return;
75     }
76     markupObjectProblems ($rackData);
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     );
84     $rtdepth = 9;
85     $offset[0] = 3;
86     $offset[1] = 3 + $rtwidth[0];
87     $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
88     $totalheight = 3 + 3 + $rackData['height'] * 2;
89     $totalwidth = $offset[2] + $rtwidth[2] + 3;
90     $img = @imagecreatetruecolor ($totalwidth, $totalheight)
91         or die("Cannot Initialize new GD image stream");
92     // cache our palette as well
93     $color = array();
94     foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
95         $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
96     $color['black'] = colorFromHex ($img, '000000');
97     $color['gray'] = colorFromHex ($img, 'c0c0c0');
98     imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
99     imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
100     imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
101     for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
102     {
103         for ($locidx = 0; $locidx < 3; $locidx++)
104         {
105             $colorcode = $rackData[$unit_no][$locidx]['state'];
106             if (isset ($rackData[$unit_no][$locidx]['hl']))
107                 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
108             imagerectangle
109             (
110                 $img,
111                 $offset[$locidx],
112                 3 + ($rackData['height'] - $unit_no) * 2,
113                 $offset[$locidx] + $rtwidth[$locidx] - 1,
114                 3 + ($rackData['height'] - $unit_no) * 2 + 1,
115                 $color[$colorcode]
116             );
117         }
118     }
119     imagepng ($img);
120     imagedestroy ($img);
121 }
122
123 function renderProgressBarImage ($done)
124 {
125     $img = @imagecreatetruecolor (100, 10);
126     switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
127     {
128         case 'sparenetwork':
129             $color['T'] = colorFromHex ($img, '808080');
130             $color['F'] = colorFromHex ($img, 'c0c0c0');
131             break;
132         case 'rackspace': // teal
133         default:
134             $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
135             $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
136     }
137     imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
138     imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
139     header("Content-type: image/png");
140     imagepng ($img);
141     imagedestroy ($img);
142 }
143
144 function renderAccessDeniedImage ()
145 {
146     $img = @imagecreatetruecolor (1, 1);
147     imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
148     header("Content-type: image/png");
149     imagepng ($img);
150     imagedestroy ($img);
151 }
152
153 ?>
154
Note: See TracBrowser for help on using the browser.