YouTube is growing like anything and we often needs to integrate the YouTube videos into the websites. Embed code is not a preferred choice so most of the times, we need to use the YouTube API in order to fetch the contents and display on the website. So here, we are presenting a way to fetch YouTube Video Thumbnails dynamically for your PHP based website.
YouTube basically creates a lots of different types of thumbs on its server to be used on different devices. You can access these thumbs by using the video_ID of the YouTube video and also you can display the images on the website using a variable $link which holds the id of the video and substituting it in the place for video_ID in the link.
Mostly, Each YouTube video has four generated images. They are predictably formatted as follows:
Low Quality Thumbnail:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/sddefault.jpg
Medium Quality Thumbnail:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg
High Quality Thumbnail:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/hqdefault.jpg
Maximum Quality Thumbnail:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/maxresdefault.jpg
So this is the basic understanding of how it works. But how to achieve this through PHP?
You can use YouTube Data API to retrieve video thumbnails. API version 3 requires a key*. Obtain the key and create a videos: list request.
Example PHP Code
$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=T0Jqdjbed40"); $json = json_decode($data); var_dump($json->items[0]->snippet->thumbnails);
Decode the output and you will able to use the YouTube Video Thumbnail easily whenever you want.
And here is the handy PHP function to fetch YouTube Video Thumbnail Images:
function downloadYouTubeThubnailImage($youTubeLink='',$thumbNamilQuality='',$fileNameWithExt='',$fileDownLoadPath='') { $videoIdExploded = explode('?v=', $youTubeLink); if ( sizeof($videoIdExploded) == 1) { $videoIdExploded = explode('&v=', $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode('&',$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); }else{ $videoIdExploded = explode('?v=', $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode('&',$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); } switch ($thumbNamilQuality) { case 'LOW': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/sddefault.jpg'; break; case 'MEDIUM': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/mqdefault.jpg'; break; case 'HIGH': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/hqdefault.jpg'; break; case 'MAXIMUM': $imageUrl = 'http://img.youtube.com/vi/'.$youTubeVideoId.'/maxresdefault.jpg'; break; default: return 'Choose The Quality Between [ LOW (or) MEDIUM (or) HIGH (or) MAXIMUM]'; break; } if( empty($fileNameWithExt) || is_null($fileNameWithExt) || $fileNameWithExt === '') { $toArray = explode('/',$imageUrl); $fileNameWithExt = md5( time().mt_rand( 1,10 ) ).'.'.substr(strrchr(end($toArray),'.'),1); } if (! is_dir($fileDownLoadPath)) { mkdir($fileDownLoadPath,0777,true); } file_put_contents($fileDownLoadPath.$fileNameWithExt, file_get_contents($imageUrl)); return $fileNameWithExt; }