Advertisement
phpaddict

Remove YouTube black area from thumbnail

Apr 30th, 2015
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. function get_youtube_video_image_with_no_black_borders($youtube_id, $temp_image_location, $resized_image_location) {
  4.     // settings
  5.     $youtube_maxres_img_url = 'http://img.youtube.com/vi/' . $youtube_id . '/maxresdefault.jpg';
  6.     $youtube_default_img_url = 'http://img.youtube.com/vi/' . $youtube_id . '/mqdefault.jpg';
  7.     $less_than_hex = hexdec('222222'); // because youtube image isn't all black bordered, this is the maximum hex allowed color. this color will be cut if found on x/y axis on starting/ending points of an image
  8.  
  9.     // get remote image
  10.     $orig_image = @file_get_contents($youtube_maxres_img_url);
  11.     if ($orig_image===false) { // if the video is smaller size, get the smaller thumb
  12.         $orig_image = @file_get_contents($youtube_default_img_url);
  13.     }
  14.  
  15.     // save it locally
  16.     file_put_contents($temp_image_location, $orig_image);
  17.     unset($orig_image);
  18.  
  19.     // get the image size
  20.     list($width, $height) = getimagesize($temp_image_location);
  21.  
  22.     // calculate y axis start point
  23.     $image_res = imagecreatefromjpeg($temp_image_location);
  24.     $y = 0;
  25.     $min_y = -1;
  26.     $start_color = imagecolorat($image_res, 0, 0);
  27.  
  28.     $one_color = true;
  29.     while ($one_color && $y < $height) {
  30.         for ($x = 0; $x < $width; $x++) {
  31.             $rgb = imagecolorat($image_res, $x, $y);
  32.             if($rgb > $less_than_hex) $one_color = false;
  33.         }
  34.         $min_y++;
  35.         $y++;
  36.     }
  37.  
  38.     // calculate y axis stop point
  39.     $y = $height - 1;
  40.     $max_y = $height + 1;
  41.     $start_color = imagecolorat($image_res, $width - 1, $y);
  42.     $one_color = true;
  43.     while ($one_color && $y > 0) {
  44.         for ($x = 0; $x < $width; $x++) {
  45.             $rgb = imagecolorat($image_res, $x, $y);
  46.             if($rgb > $less_than_hex) $one_color = false;
  47.         }
  48.         $max_y--;
  49.         $y--;
  50.     }
  51.  
  52.     // calculate x axis start point
  53.     $start_color = imagecolorat($image_res, 0, 0);
  54.     $x = 0;
  55.     $min_x = - 1;
  56.     $one_color = true;
  57.     while ($one_color && $x < $width) {
  58.         for ($y = $min_y; $y < $max_y; $y++) {
  59.             $rgb = imagecolorat($image_res, $x, $y);
  60.             if($rgb > $less_than_hex) $one_color = false;
  61.         }
  62.         $min_x++;
  63.         $x++;
  64.     }
  65.  
  66.     // calculate x axis stop point
  67.     $x = $width - 1;
  68.     $start_color = imagecolorat($image_res, $width - 1, $height - 1);
  69.     $max_x = $width + 1;
  70.     $one_color = true;
  71.     while ($one_color && $x > 0) {
  72.         for ($y = $min_y; $y < $max_y; $y++) {
  73.             $rgb = imagecolorat($image_res, $x, $y);
  74.             if($rgb > $less_than_hex) $one_color = false;
  75.         }
  76.         $max_x--;
  77.         $x--;
  78.     }
  79.  
  80.     // calculate new width/height
  81.     $new_height = $max_y - $min_y;
  82.     $new_width = $max_x - $min_x;
  83.  
  84.     // crop the image
  85.     $gd = imagecreatetruecolor($new_width, $new_height);
  86.     imagecopy($gd, $image_res, 0, 0, $min_x, $min_y, $width, $height);
  87.  
  88.     // save the new image to a different location
  89.     imagejpeg($gd, $resized_image_location, 100);
  90.  
  91.     // delete the temporary file
  92.     unlink($temp_image_location);
  93. }
  94.  
  95. /*
  96. Usage:
  97.  
  98. $youtube_id = 'y6Sxv-sUYtM';
  99. $temp_directory = '/tmp/' . microtime(true) . '_youtube_file.jpg';
  100. $destination_image = '/home/user/thumbs/' . microtime(true) . '_youtube_cropped_file.jpg';
  101. get_youtube_video_image_with_no_black_borders($youtube_id, $temp_directory, $destination_image);
  102.  
  103. Notes:
  104.     * the function doesn't take into consideration if the image screenshot of the video is all black
  105. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement