root/tags/RackTables-0.15.1/render_image.php

Revision 1838, 3.8 kB (checked in by pilot, 10 months ago)

+ pass and handle caller function name for assertions

Line 
1 <?php
2
3 // This page outputs PNG rack thumbnail.
4
5 require 'inc/init.php';
6
7 assertStringArg ('img', __FUNCTION__);
8 switch ($_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', __FUNCTION__);
16         renderRackThumb ($_REQUEST['rack_id']);
17         break;
18     case 'progressbar':
19         assertUIntArg ('done', __FUNCTION__, 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)
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     $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;
89     $img = @imagecreatetruecolor ($totalwidth, $totalheight)
90         or die("Cannot Initialize new GD image stream");
91     // cache our palette as well
92     $color = array();
93     foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
94         $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
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,
112                 $color[$colorcode]
113             );
114         }
115     }
116     imagepng ($img);
117     imagedestroy ($img);
118 }
119
120 function renderProgressBarImage ($done)
121 {
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']);
127     header("Content-type: image/png");
128     imagepng ($img);
129     imagedestroy ($img);
130 }
131
132 ?>
133
Note: See TracBrowser for help on using the browser.