PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/render_image.php

https://github.com/mcr/racktables
PHP | 228 lines | 204 code | 12 blank | 12 comment | 15 complexity | 10885e03c004d4396492a0c0272aaaa2 MD5 | raw file
  1. <?php
  2. ob_start();
  3. try {
  4. require 'inc/init.php';
  5. assertStringArg ('img');
  6. switch ($_REQUEST['img'])
  7. {
  8. case 'minirack': // rack security context
  9. assertUIntArg ('rack_id');
  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', TRUE);
  20. renderProgressBarImage ($_REQUEST['done']);
  21. break;
  22. case 'preview': // file security context
  23. assertUIntArg ('file_id');
  24. $pageno = 'file';
  25. $tabno = 'download';
  26. fixContext();
  27. if (!permitted())
  28. renderAccessDeniedImage();
  29. else
  30. renderFilePreview ($_REQUEST['file_id'], $_REQUEST['img']);
  31. break;
  32. default:
  33. renderError();
  34. }
  35. ob_end_flush();
  36. }
  37. catch (Exception $e)
  38. {
  39. ob_end_clean();
  40. renderError();
  41. }
  42. //------------------------------------------------------------------------
  43. function renderError ()
  44. {
  45. // A hardcoded value is worth of saving lots of code here.
  46. $img = imagecreatefrompng ('pix/error.png');
  47. header("Content-type: image/png");
  48. imagepng ($img);
  49. imagedestroy ($img);
  50. }
  51. // Having a local caching array speeds things up. A little.
  52. function colorFromHex ($image, $hex)
  53. {
  54. static $colorcache = array ();
  55. if (isset ($colorcache[$hex]))
  56. return $colorcache[$hex];
  57. $r = hexdec ('0x' . substr ($hex, 0, 2));
  58. $g = hexdec ('0x' . substr ($hex, 2, 2));
  59. $b = hexdec ('0x' . substr ($hex, 4, 2));
  60. $c = imagecolorallocate ($image, $r, $g, $b);
  61. $colorcache[$hex] = $c;
  62. return $c;
  63. }
  64. function renderRackThumb ($rack_id = 0)
  65. {
  66. // Don't call DB extra times, hence we are most probably not the
  67. // only script wishing to acces the same data now.
  68. if (NULL !== ($thumbcache = loadThumbCache ($rack_id)))
  69. {
  70. header("Content-type: image/png");
  71. echo $thumbcache;
  72. return;
  73. }
  74. ob_start();
  75. if (FALSE !== generateMiniRack ($rack_id))
  76. {
  77. $capture = ob_get_clean();
  78. header("Content-type: image/png");
  79. echo $capture;
  80. saveThumbCache ($rack_id, $capture);
  81. return;
  82. }
  83. // error text in the buffer
  84. ob_end_flush();
  85. }
  86. // Output a binary string containing the PNG minirack. Indicate error with return code.
  87. function generateMiniRack ($rack_id)
  88. {
  89. if (NULL === ($rackData = spotEntity ('rack', $rack_id)))
  90. return FALSE;
  91. amplifyCell ($rackData);
  92. markupObjectProblems ($rackData);
  93. global $rtwidth;
  94. $rtdepth = 9;
  95. $offset[0] = 3;
  96. $offset[1] = 3 + $rtwidth[0];
  97. $offset[2] = 3 + $rtwidth[0] + $rtwidth[1];
  98. $totalheight = 3 + 3 + $rackData['height'] * 2;
  99. $totalwidth = $offset[2] + $rtwidth[2] + 3;
  100. $img = @imagecreatetruecolor ($totalwidth, $totalheight)
  101. or die("Cannot Initialize new GD image stream");
  102. // cache our palette as well
  103. $color = array();
  104. foreach (array ('F', 'A', 'U', 'T', 'Th', 'Tw', 'Thw') as $statecode)
  105. $color[$statecode] = colorFromHex ($img, getConfigVar ('color_' . $statecode));
  106. $color['black'] = colorFromHex ($img, '000000');
  107. $color['gray'] = colorFromHex ($img, 'c0c0c0');
  108. imagerectangle ($img, 0, 0, $totalwidth - 1, $totalheight - 1, $color['black']);
  109. imagerectangle ($img, 1, 1, $totalwidth - 2, $totalheight - 2, $color['gray']);
  110. imagerectangle ($img, 2, 2, $totalwidth - 3, $totalheight - 3, $color['black']);
  111. for ($unit_no = 1; $unit_no <= $rackData['height']; $unit_no++)
  112. {
  113. for ($locidx = 0; $locidx < 3; $locidx++)
  114. {
  115. $colorcode = $rackData[$unit_no][$locidx]['state'];
  116. if (isset ($rackData[$unit_no][$locidx]['hl']))
  117. $colorcode = $colorcode . $rackData[$unit_no][$locidx]['hl'];
  118. imagerectangle
  119. (
  120. $img,
  121. $offset[$locidx],
  122. 3 + ($rackData['height'] - $unit_no) * 2,
  123. $offset[$locidx] + $rtwidth[$locidx] - 1,
  124. 3 + ($rackData['height'] - $unit_no) * 2 + 1,
  125. $color[$colorcode]
  126. );
  127. }
  128. }
  129. imagepng ($img);
  130. imagedestroy ($img);
  131. return TRUE;
  132. }
  133. function renderProgressBarImage ($done)
  134. {
  135. $img = @imagecreatetruecolor (100, 10);
  136. switch (isset ($_REQUEST['theme']) ? $_REQUEST['theme'] : 'rackspace')
  137. {
  138. case 'sparenetwork':
  139. $color['T'] = colorFromHex ($img, '808080');
  140. $color['F'] = colorFromHex ($img, 'c0c0c0');
  141. break;
  142. case 'rackspace': // teal
  143. default:
  144. $color['T'] = colorFromHex ($img, getConfigVar ('color_T'));
  145. $color['F'] = colorFromHex ($img, getConfigVar ('color_F'));
  146. }
  147. imagefilledrectangle ($img, 0, 0, $done, 10, $color['T']);
  148. imagefilledrectangle ($img, $done, 0, 100, 10, $color['F']);
  149. for ($x = 20; $x <= 80; $x += 20)
  150. {
  151. $cc = $x > $done ? $color['T'] : $color['F'];
  152. imagesetpixel ($img, $x, 0, $cc);
  153. imagesetpixel ($img, $x, 1, $cc);
  154. imagesetpixel ($img, $x, 4, $cc);
  155. imagesetpixel ($img, $x, 5, $cc);
  156. imagesetpixel ($img, $x, 8, $cc);
  157. imagesetpixel ($img, $x, 9, $cc);
  158. }
  159. header("Content-type: image/png");
  160. imagepng ($img);
  161. imagedestroy ($img);
  162. }
  163. function renderAccessDeniedImage ()
  164. {
  165. $img = @imagecreatetruecolor (1, 1);
  166. imagefilledrectangle ($img, 0, 0, 1, 1, colorFromHex ($img, '000000'));
  167. header("Content-type: image/png");
  168. imagepng ($img);
  169. imagedestroy ($img);
  170. die;
  171. }
  172. function renderFilePreview ($file_id = 0, $mode = 'view')
  173. {
  174. switch ($mode)
  175. {
  176. case 'view':
  177. // GFX files can become really big, if we uncompress them in memory just to
  178. // provide a PNG version of a file. To keep things working, just send the
  179. // contents as is for known MIME types.
  180. $file = getFile ($file_id);
  181. if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
  182. {
  183. showError ('Invalid MIME type on file', __FUNCTION__);
  184. break;
  185. }
  186. header("Content-type: ${file['type']}");
  187. echo $file['contents'];
  188. break;
  189. case 'preview':
  190. $file = getFile ($file_id);
  191. $image = imagecreatefromstring ($file['contents']);
  192. unset ($file);
  193. $width = imagesx ($image);
  194. $height = imagesy ($image);
  195. if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
  196. {
  197. // TODO: cache thumbs for faster page generation
  198. $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
  199. $newwidth = $width * $ratio;
  200. $newheight = $height * $ratio;
  201. $resampled = imagecreatetruecolor ($newwidth, $newheight);
  202. imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  203. imagedestroy ($image);
  204. $image = $resampled;
  205. unset ($resampled);
  206. }
  207. header("Content-type: image/png"); // don't announce content-length, it may have changed after resampling
  208. imagepng ($image);
  209. imagedestroy ($image);
  210. break;
  211. default:
  212. showError ('Invalid argument', __FUNCTION__);
  213. break;
  214. }
  215. }
  216. ?>