You Tube provides two types (small/large) of thumbnails for each video. It quite simple task to grab thumbnail from YouTube video. I created this function for grabbing thumbnails form YouTube videos. Its able to grab from both URL and Embedded code and also small and large size.

Function Body:

/**
* Function: youtube thumb grabber
*
* @description :
* @param  $ : video code, url type (embed/url),size (small/Large),thumb link
* @return : thumb path
* @author : Mamun.
* @last -modified-by: Mamun.
*/
if (! function_exists('youtube_thumb_grabber' ))
{

     function youtube_thumb_grabber($video_code, $link_type = "embed", $size = "small", $thumb_link = "")
    {
            if ($video_code != '')
            {
                   if ($link_type == "embed")
                   {

                                  $splited_data = explode("=",$video_code);

                                  $video_unique_code = substr(strrchr($splited_data[4],"/"),1,-strlen(strrchr($splited_data[4],"&")));

                  }
                  else if ($link_type == "url")
                  {
                                  $splited_data = explode("=",$video_code);
                                  $video_unique_code = substr($splited_data[1],0,-strlen(strrchr($splited_data[1],"&")));
                  }
                  else
                  {
                                  return;
                  }

                  if($size == "small")
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/2.jpg\" alt=\"No image\" /></a>";
                  }
                  else if ($size == "large")
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/0.jpg\" alt=\"No image\" /></a>";
                  }
                  else
                  {
                            return "<a href=\"$thumb_link\"><img src=\"http://img.youtube.com/vi/$video_unique_code/2.jpg\" alt=\"No image\" /></a>";

                   }

             }

      }
}

Calling Function:

<?php 

echo youtube_thumb_grabber($video["video_code"],"embed","small","resources/videos/view/".$video["video_title_url"]);

?>

Its Cool. 😀

Advertisements