Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function get_youtube_video_image_with_no_black_borders($youtube_id, $temp_image_location, $resized_image_location) {
- // settings
- $youtube_maxres_img_url = 'http://img.youtube.com/vi/' . $youtube_id . '/maxresdefault.jpg';
- $youtube_default_img_url = 'http://img.youtube.com/vi/' . $youtube_id . '/mqdefault.jpg';
- $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
- // get remote image
- $orig_image = @file_get_contents($youtube_maxres_img_url);
- if ($orig_image===false) { // if the video is smaller size, get the smaller thumb
- $orig_image = @file_get_contents($youtube_default_img_url);
- }
- // save it locally
- file_put_contents($temp_image_location, $orig_image);
- unset($orig_image);
- // get the image size
- list($width, $height) = getimagesize($temp_image_location);
- // calculate y axis start point
- $image_res = imagecreatefromjpeg($temp_image_location);
- $y = 0;
- $min_y = -1;
- $start_color = imagecolorat($image_res, 0, 0);
- $one_color = true;
- while ($one_color && $y < $height) {
- for ($x = 0; $x < $width; $x++) {
- $rgb = imagecolorat($image_res, $x, $y);
- if($rgb > $less_than_hex) $one_color = false;
- }
- $min_y++;
- $y++;
- }
- // calculate y axis stop point
- $y = $height - 1;
- $max_y = $height + 1;
- $start_color = imagecolorat($image_res, $width - 1, $y);
- $one_color = true;
- while ($one_color && $y > 0) {
- for ($x = 0; $x < $width; $x++) {
- $rgb = imagecolorat($image_res, $x, $y);
- if($rgb > $less_than_hex) $one_color = false;
- }
- $max_y--;
- $y--;
- }
- // calculate x axis start point
- $start_color = imagecolorat($image_res, 0, 0);
- $x = 0;
- $min_x = - 1;
- $one_color = true;
- while ($one_color && $x < $width) {
- for ($y = $min_y; $y < $max_y; $y++) {
- $rgb = imagecolorat($image_res, $x, $y);
- if($rgb > $less_than_hex) $one_color = false;
- }
- $min_x++;
- $x++;
- }
- // calculate x axis stop point
- $x = $width - 1;
- $start_color = imagecolorat($image_res, $width - 1, $height - 1);
- $max_x = $width + 1;
- $one_color = true;
- while ($one_color && $x > 0) {
- for ($y = $min_y; $y < $max_y; $y++) {
- $rgb = imagecolorat($image_res, $x, $y);
- if($rgb > $less_than_hex) $one_color = false;
- }
- $max_x--;
- $x--;
- }
- // calculate new width/height
- $new_height = $max_y - $min_y;
- $new_width = $max_x - $min_x;
- // crop the image
- $gd = imagecreatetruecolor($new_width, $new_height);
- imagecopy($gd, $image_res, 0, 0, $min_x, $min_y, $width, $height);
- // save the new image to a different location
- imagejpeg($gd, $resized_image_location, 100);
- // delete the temporary file
- unlink($temp_image_location);
- }
- /*
- Usage:
- $youtube_id = 'y6Sxv-sUYtM';
- $temp_directory = '/tmp/' . microtime(true) . '_youtube_file.jpg';
- $destination_image = '/home/user/thumbs/' . microtime(true) . '_youtube_cropped_file.jpg';
- get_youtube_video_image_with_no_black_borders($youtube_id, $temp_directory, $destination_image);
- Notes:
- * the function doesn't take into consideration if the image screenshot of the video is all black
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement