Posted By

madtrader117 on 08/09/10


Tagged

thumbnails aspect border ffmpeg ratio black


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bcmoney


Thumbnails: Find Black Border Size of Movie Screenshot


 / Published in: PHP
 

This function takes an image and finds out if there is a black border on either side of the image. This is helpful for making thumbnails with FFMpeg out of movies where the author has used black padding to maintain aspect ratio.

  1. <?php
  2. define("DEBUG_OUT",TRUE);
  3. $border_size = find_border_size($path);
  4. print_r($border_size);
  5. /*
  6.   * @desc Find the black border size of a jpeg image.
  7.   * @author Michael Campbell [email protected]
  8.   * @param string path to the image you want to use
  9.   * @return array (border_width, border_height)
  10.   *
  11.   * Function takes a single param, the path to an image, and will scan the image from
  12.   * top to bottom as well as left to right to find the black area left by FFMpeg when
  13.   * creating thumbnails of movies where the creator has used padding to create a certain
  14.   * aspect ratio. There may be a better way to do this but I found it necessary for aesthetic
  15.   * reasons.
  16.   *
  17.   * If you plan on doing the cropping yourself be sure to add the border size to all sides
  18.   * in order to keep the original aspect ratio.
  19.   *
  20.   * If you are using FFMpeg here is what I do to make 240x180 thumbnails.
  21.   * Let's say we have a 320x240 movie that has a 40px top border and 15px side
  22.   * border. You simply need to take the larger value since cropping with it will
  23.   * encompass the other.
  24.   *
  25.   * $border = max(find_border_size("img.jpg"));
  26.   * $thumb_size_x = 180+(2*$border);
  27.   * $thumb_size_y = 240+(2*$border);
  28.   * $thumb_size = $thumb_size_x . "x" . $thumb_size_y; (String to use in the -s param of ffmpeg)
  29.   * $crop_cmd = "-croptop $border -cropbottom $border -cropright $border -cropleft $border";
  30.   */
  31. function find_border_size($path)
  32. {
  33. /* The pad var is essentially a 'feather' value. Unless one of the RGB values rises
  34.   * above $pad we are saying to continue. You can try different values but with 10 I
  35.   * would still get a decent sized border due to the bleedover of the movie into
  36.   * the hat.
  37.   */
  38. $pad = 20;
  39. $border_y = 0;
  40. $border_x = 0;
  41.  
  42. if(!file_exists($path))
  43. {
  44. if(DEBUG_OUT) echo("Error: $path not found.\n");
  45. return FALSE;
  46. }
  47. else
  48. {
  49. if(DEBUG_OUT) echo("Opening: $path ...\n");
  50. }
  51. $im = @imagecreatefromjpeg($path);
  52. if(!$im) return FALSE;
  53. $height = imagesy($im);
  54. $width = imagesx($im);
  55.  
  56. /* Let's start at 0, 0 and keep going till we hit a color */
  57. if(DEBUG_OUT) echo("Image - Height: $height / Width: $width\n");
  58. /* Border Height(Y) */
  59. $center_width = ceil($width/2);
  60. for($i=0; $i<$height; $i++)
  61. {
  62. $rgb = imagecolorat($im,$center_width,$i);
  63. $r = ($rgb >> 16) & 0xFF;
  64. $g = ($rgb >> 8) & 0xFF;
  65. $b = $rgb & 0xFF;
  66. if(DEBUG_OUT) echo("Height: ($center_width,$i) R: $r / G: $g / B: $b\n");
  67. if($r >= $pad || $g >= $pad || $b >= $pad)
  68. {
  69. $border_y = $i;
  70. if($border_y == $height) $border_y = 0;
  71. break;
  72. }
  73. }
  74.  
  75. /* Border Width(X) */
  76. $center_height = ceil($height/2);
  77. for($i=0; $i<$width; $i++)
  78. {
  79. $rgb = imagecolorat($im,$i,$center_height);
  80. $r = ($rgb >> 16) & 0xFF;
  81. $g = ($rgb >> 8) & 0xFF;
  82. $b = $rgb & 0xFF;
  83. if(DEBUG_OUT) echo("Width: ($i,$center_width) R: $r / G: $g / B: $b\n");
  84. if($r >= $pad || $g >= $pad || $b >= $pad)
  85. {
  86. $border_x = $i;
  87. if($border_x == $width) $border_x = 0;
  88. break;
  89. }
  90. }
  91.  
  92. /* I am making the border a multiple of 2 since I am sending these values to FFMpeg */
  93. if($border_x != 0)
  94. {
  95. $border_x /= 2;
  96. $border_x = round($border_x);
  97. $border_x *= 2;
  98. }
  99. if($border_y != 0)
  100. {
  101. $border_y /= 2;
  102. $border_y = round($border_y);
  103. $border_y *= 2;
  104. }
  105. if(DEBUG_OUT) echo("Border Width: $border_x / Border Height: $border_y\n");
  106. return array($border_x,$border_y);
  107. }
  108. ?>

Report this snippet  

You need to login to post a comment.