3 require 'inc/init.php';
5 assertStringArg ('img', __FILE__
);
6 switch ($_REQUEST['img'])
8 case 'minirack': // rack security context
9 assertUIntArg ('rack_id', __FILE__
);
14 renderAccessDeniedImage();
16 renderRackThumb ($_REQUEST['rack_id']);
18 case 'progressbar': // no security context
19 assertUIntArg ('done', __FILE__
, TRUE);
20 renderProgressBarImage ($_REQUEST['done']);
22 case 'view': // file security context
24 assertUIntArg ('file_id', __FILE__
);
29 renderAccessDeniedImage();
31 renderFilePreview ($_REQUEST['file_id']);
37 //------------------------------------------------------------------------
38 function renderError ()
40 // A hardcoded value is worth of saving lots of code here.
41 $img = imagecreatefrompng ('pix/error.png');
42 header("Content-type: image/png");
47 // Having a local caching array speeds things up. A little.
48 function colorFromHex ($image, $hex)
50 static $colorcache = array ();
51 if (isset ($colorcache[$hex]))
52 return $colorcache[$hex];
53 $r = hexdec ('0x' . substr ($hex, 0, 2));
54 $g = hexdec ('0x' . substr ($hex, 2, 2));
55 $b = hexdec ('0x' . substr ($hex, 4, 2));
56 $c = imagecolorallocate ($image, $r, $g, $b);
57 $colorcache[$hex] = $c;
61 function renderRackThumb ($rack_id = 0)
63 // Don't call DB extra times, hence we are most probably not the
64 // only script wishing to acces the same data now.
65 header("Content-type: image/png");
66 $thumbcache = loadThumbCache ($rack_id);
67 if ($thumbcache !== NULL)
72 generateMiniRack ($rack_id);
73 $capture = ob_get_contents();
75 saveThumbCache ($rack_id, $capture);
79 // Output a binary string containing the PNG minirack.
80 function generateMiniRack ($rack_id = 0)
82 if (($rackData = getRackData ($rack_id, TRUE)) == NULL)
87 markupObjectProblems ($rackData);
91 $offset[1] = 3 +
$rtwidth[0];
92 $offset[2] = 3 +
$rtwidth[0] +
$rtwidth[1];
93 $totalheight = 3 +
3 +
$rackData['height'] * 2;
94 $totalwidth = $offset[2] +
$rtwidth[2] +
3;
95 $img = @imagecreatetruecolor
($totalwidth, $totalheight)
96 or die("Cannot Initialize new GD image stream");
97 // cache our palette as well
99 foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
100 $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
101 $color['black'] = colorFromHex ($img, '000000');
102 $color['gray'] = colorFromHex ($img, 'c0c0c0');
103 imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
104 imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
105 imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
106 for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++
)
108 for ($locidx = 0; $locidx < 3; $locidx++
)
110 $colorcode = $rackData[$unit_no][$locidx]['state'];
111 if (isset ($rackData[$unit_no][$locidx]['hl']))
112 $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
117 3 +
($rackData['height'] - $unit_no) * 2,
118 $offset[$locidx] +
$rtwidth[$locidx] - 1,
119 3 +
($rackData['height'] - $unit_no) * 2 +
1,
128 function renderProgressBarImage ($done)
130 $img = @imagecreatetruecolor
(100, 10);
131 switch (isset ($_REQUEST['theme']) ?
$_REQUEST['theme'] : 'rackspace')
134 $color['T'] = colorFromHex ($img, '808080');
135 $color['F'] = colorFromHex ($img, 'c0c0c0');
137 case 'rackspace': // teal
139 $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
140 $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
142 imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
143 imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
144 header("Content-type: image/png");
149 function renderAccessDeniedImage ()
151 $img = @imagecreatetruecolor
(1, 1);
152 imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
153 header("Content-type: image/png");
158 function renderFilePreview ($file_id = 0)
160 $file = getFile ($file_id);
161 $image = imagecreatefromstring ($file['contents']);
162 $width = imagesx ($image);
163 $height = imagesy ($image);
164 if ($_REQUEST['img'] == 'preview' and ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS')))
166 // TODO: cache thumbs for faster page generation
167 $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
168 $newwidth = $width * $ratio;
169 $newheight = $height * $ratio;
170 $resampled = imagecreatetruecolor ($newwidth, $newheight);
171 imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
172 imagedestroy ($image);
176 header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
178 imagedestroy ($image);