Get YouTube video thumbnail and use it with PHP
YouTube stores many different types of thumbnails on its server for different devices. You can access it by using the video id which
every YouTube video has. You can display the images on your website using a variable $link
which holds the id of the video and substituting it
in the place for video_ID in the link.
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
Example:
If you want to access the thumbnail of the following video:
https://www.youtube.com/watch?v=Q-GYwhqDo6o
Video ID : Q-GYwhqDo6o
So, this is how video thumbnail link looks like:
http://img.youtube.com/vi/Q-GYwhqDo6o/mqdefault.jpg
Hope it helps. Enjoy coding.
You can use the below code. It is work for me. Choose the image quality as per your requirement.
<?php
$youtubeID = getYouTubeVideoId('youtube video url');
$thumbURL = 'https://img.youtube.com/vi/' . $youtubeID . '/mqdefault.jpg';
print_r($thumbURL);
function getYouTubeVideoId($pageVideUrl) {
$link = $pageVideUrl;
$video_id = explode("?v=", $link);
if (!isset($video_id[1])) {
$video_id = explode("youtu.be/", $link);
}
$youtubeID = $video_id[1];
if (empty($video_id[1])) $video_id = explode("/v/", $link);
$video_id = explode("&", $video_id[1]);
$youtubeVideoID = $video_id[0];
if ($youtubeVideoID) {
return $youtubeVideoID;
} else {
return false;
}
}
?>
To get high-quality image you can use the following URL which is fetched from youtube API
$video_id = explode("?v=", $link);
$video_id = $video_id[1];
$thumbnail="http://img.youtube.com/vi/".$video_id."/maxresdefault.jpg";