PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 14ms app.codeStats 0ms

/dist/webpagetest/www/video/filmstrip.php

http://webpagetest.googlecode.com/
PHP | 243 lines | 201 code | 29 blank | 13 comment | 37 complexity | 5bc0e98d144bc29689437318b275ed01 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-3.0, LGPL-3.0, MIT, BSD-3-Clause, ISC, LGPL-2.1
  1. <?php
  2. header('Content-disposition: attachment; filename=filmstrip.png');
  3. header ("Content-type: image/png");
  4. chdir('..');
  5. include 'common.inc';
  6. require_once('page_data.inc');
  7. require_once('draw.inc');
  8. include 'video/filmstrip.inc.php'; // include the commpn php shared across the filmstrip code
  9. $colMargin = 5;
  10. $rowMargin = 5;
  11. $font = 4;
  12. $fontWidth = imagefontwidth($font);
  13. $fontHeight = imagefontheight($font);
  14. $thumbTop = $fontHeight + $rowMargin;
  15. $bgcolor = '000000';
  16. $color = 'ffffff';
  17. if (array_key_exists('bg', $_GET)) {
  18. $bgcolor = $_GET['bg'];
  19. }
  20. if (array_key_exists('text', $_GET)) {
  21. $color = $_GET['text'];
  22. }
  23. $bgcolor = html2rgb($bgcolor);
  24. $color = html2rgb($color);
  25. // go through each of the tests and figure out the width/height
  26. $columnWidth = 0;
  27. foreach( $tests as &$test ) {
  28. if( $test['video']['width'] && $test['video']['height'] ) {
  29. if( $test['video']['width'] > $test['video']['height'] ) {
  30. $test['video']['thumbWidth'] = $thumbSize;
  31. $test['video']['thumbHeight'] = (int)(((float)$thumbSize / (float)$test['video']['width']) * (float)$test['video']['height']);
  32. } else {
  33. $test['video']['thumbHeight'] = $thumbSize;
  34. $test['video']['thumbWidth'] = (int)(((float)$thumbSize / (float)$test['video']['height']) * (float)$test['video']['width']);
  35. }
  36. if ($test['video']['thumbWidth'] > $columnWidth) {
  37. $columnWidth = $test['video']['thumbWidth'];
  38. }
  39. }
  40. }
  41. // figure out the label width
  42. $labelWidth = 0;
  43. foreach( $tests as &$test )
  44. {
  45. $test['label'] = $test['name'];
  46. $len = strlen($test['label']);
  47. if( $len > 30 )
  48. {
  49. $test['label'] = substr($test['label'], 0, 27) . '...';
  50. $len = strlen($test['label']);
  51. }
  52. if( $len > $labelWidth )
  53. $labelWidth = $len;
  54. }
  55. $labelWidth = $labelWidth * $fontWidth;
  56. $thumbLeft = $labelWidth + $colMargin;
  57. // figure out how many columns there are
  58. $end = 0;
  59. foreach( $tests as &$test )
  60. if( $test['video']['end'] > $end )
  61. $end = $test['video']['end'];
  62. // figure out the size of the resulting image
  63. $width = $thumbLeft + $colMargin;
  64. $count = 0;
  65. $skipped = $interval;
  66. $last = $end + $interval - 1;
  67. for( $frame = 0; $frame <= $last; $frame++ )
  68. {
  69. $skipped++;
  70. if( $skipped >= $interval )
  71. {
  72. $skipped = 0;
  73. $count++;
  74. }
  75. }
  76. $width += ($columnWidth + ($colMargin * 2)) * $count;
  77. // figure out the height of each row
  78. $height = $fontHeight + ($rowMargin * 2);
  79. foreach( $tests as &$test ) {
  80. $height += $test['video']['thumbHeight'] + ($rowMargin * 2);
  81. }
  82. // create the blank image
  83. $im = imagecreatetruecolor($width, $height);
  84. // define some colors
  85. $background = GetColor($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
  86. $textColor = GetColor($im, $color[0], $color[1], $color[2]);
  87. $colChanged = GetColor($im, 254,179,1);
  88. $colAFT = GetColor($im, 255,0,0);
  89. imagefilledrectangle($im, 0, 0, $width, $height, $background);
  90. // put the time markers across the top
  91. $left = $thumbLeft;
  92. $top = $thumbTop - $fontHeight;
  93. $skipped = $interval;
  94. $last = $end + $interval - 1;
  95. for( $frame = 0; $frame <= $last; $frame++ )
  96. {
  97. $skipped++;
  98. if( $skipped >= $interval )
  99. {
  100. $left += $colMargin;
  101. $skipped = 0;
  102. $val = number_format((float)$frame / 10.0, 1) . 's';
  103. $x = $left + (int)($columnWidth / 2.0) - (int)((double)$fontWidth * ((double)strlen($val) / 2.0));
  104. imagestring($im, $font, $x, $top, $val, $textColor);
  105. $left += $columnWidth + $colMargin;
  106. }
  107. }
  108. // draw the text labels
  109. $top = $thumbTop;
  110. $left = $colMargin;
  111. foreach( $tests as &$test )
  112. {
  113. $top += $rowMargin;
  114. $x = $left + $labelWidth - (int)(strlen($test['label']) * $fontWidth);
  115. $y = $top + (int)(($test['video']['thumbHeight'] / 2.0) - ($fontHeight / 2.0));
  116. imagestring($im, $font, $x, $y, $test['label'], $textColor);
  117. $top += $test['video']['thumbHeight'] + $rowMargin;
  118. }
  119. // fill in the actual thumbnails
  120. $top = $thumbTop;
  121. $thumb = null;
  122. foreach( $tests as &$test )
  123. {
  124. $aft = (int)$test['aft'] / 100;
  125. $left = $thumbLeft;
  126. $top += $rowMargin;
  127. $lastThumb = null;
  128. if( $thumb )
  129. {
  130. imagedestroy($thumb);
  131. unset($thumb);
  132. }
  133. $skipped = $interval;
  134. $last = $end + $interval - 1;
  135. for( $frame = 0; $frame <= $last; $frame++ )
  136. {
  137. $path = $test['video']['frames'][$frame];
  138. if( isset($path) )
  139. $test['currentframe'] = $frame;
  140. else
  141. {
  142. if( isset($test['currentframe']) )
  143. $path = $test['video']['frames'][$test['currentframe']];
  144. else
  145. $path = $test['video']['frames'][0];
  146. }
  147. if( !$lastThumb )
  148. $lastThumb = $path;
  149. $skipped++;
  150. if( $skipped >= $interval )
  151. {
  152. $skipped = 0;
  153. if( $frame - $interval + 1 <= $test['video']['end'] )
  154. {
  155. unset($border);
  156. $cached = '';
  157. if( $test['cached'] )
  158. $cached = '_cached';
  159. $imgPath = GetTestPath($test['id']) . "/video_{$test['run']}$cached/$path";
  160. if( $lastThumb != $path || !$thumb )
  161. {
  162. if( $lastThumb != $path )
  163. $border = $colChanged;
  164. if( $aft && $frame >= $aft )
  165. {
  166. $aft = 0;
  167. $border = $colAFT;
  168. }
  169. // load the new thumbnail
  170. if( $thumb )
  171. {
  172. imagedestroy($thumb);
  173. unset($thuumb);
  174. }
  175. $tmp = imagecreatefromjpeg("./$imgPath");
  176. if( $tmp )
  177. {
  178. $thumb = imagecreatetruecolor($test['video']['thumbWidth'], $test['video']['thumbHeight']);
  179. fastimagecopyresampled($thumb, $tmp, 0, 0, 0, 0, $test['video']['thumbWidth'], $test['video']['thumbHeight'], imagesx($tmp), imagesy($tmp), 4);
  180. imagedestroy($tmp);
  181. }
  182. }
  183. // draw the thumbnail
  184. $left += $colMargin;
  185. $width = imagesx($thumb);
  186. $padding = ($columnWidth - $width) / 2;
  187. if( isset($border) )
  188. imagefilledrectangle($im, $left - 2 + $padding, $top - 2, $left + imagesx($thumb) + 2 + $padding, $top + imagesy($thumb) + 2, $border);
  189. imagecopy($im, $thumb, $left + $padding, $top, 0, 0, $width, imagesy($thumb));
  190. $left += $columnWidth + $colMargin;
  191. $lastThumb = $path;
  192. }
  193. }
  194. }
  195. $top += $test['video']['thumbHeight'] + $rowMargin;
  196. }
  197. // spit the image out to the browser
  198. imagepng($im);
  199. imagedestroy($im);
  200. function html2rgb($color) {
  201. if ($color[0] == '#')
  202. $color = substr($color, 1);
  203. if (strlen($color) == 6)
  204. list($r, $g, $b) = array($color[0].$color[1],
  205. $color[2].$color[3],
  206. $color[4].$color[5]);
  207. elseif (strlen($color) == 3)
  208. list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
  209. else
  210. return false;
  211. $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
  212. return array($r, $g, $b);
  213. }
  214. ?>