Get YouTube Video Thumbnail from URL in PHP

In this tutorial, you will learn how to Get a YouTube Video thumbnail from a URL in PHP. A YouTube video thumbnail is the image that appears on a video’s listing on YouTube or video search results. A big part of why videos are clicked is because of an attractive thumbnail associated with the video link.

So, it’s important to have eye-catching thumbnails for your videos if you wish to attract more viewers. But what about getting those images from within the HTML page?

How to get YouTube Video Id from URL?

To get the thumbnail, at first you have to parse the YouTube video id from the URL. To get the id we are going to use regex. Check the below function-

function get_youtube_id($url){
	$reg = '/(?im)\b(?:https?:\/\/)?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)\/(?:(?:\??v=?i?=?\/?)|watch\?vi?=|watch\?.*?&v=|embed\/|)([A-Z0-9_-]{11})\S*(?=\s|$)/';
	
	preg_match($re, $url, $matches);
	return !$matches == Null ? $matches[1] : "";
}

//extract id from url
var_dump(get_youtube_id("https://www.youtube.com/watch?v=PgCliOxl41o"));

//output will be 
//PgCliOxl41o

Get YouTube Video Title and Thumbnail

You can use one of these below methods to get a YouTube video title and thumbnail picture.

Method 1: Get thumbnail using YouTube Data API v3

The first method of getting YouTube video title and thumbnail we are going to use the new Data API v3 introduced by Google, which provides everything that is needed to get the data along with JSON parsing.

This method will work till Google’s servers keep on serving it directly. The reason why I prefer this method over any other available one is because of because Google recommended it. Let’s look at the code below:

<?php

$video_id = get_youtube_id("https://www.youtube.com/watch?v=PgCliOxl41o");
$url = "https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=".$video_id."&key=YOUR-API-KEY";
$data = file_get_contents($url);
$json = json_decode($data);
$thumbnails = $json->items[0]->snippet->thumbnails;
$description = $json->items[0]->snippet;


//print the title
echo $description->title .'<br/>';

//get default thumbnail
echo $thumbnails->default->url .'<br/>';

//get medium thumbnail
echo $thumbnails->medium->url .'<br/>';

//get high thumbnail
echo $thumbnails->high->url .'<br/>';

//get standard thumbnail
echo $thumbnails->standard->url .'<br/>';

//get maxres thumbnail
echo $thumbnails->maxres->url;

Method 2: Get thumbnail using Image URL

If you don’t want to use the API method, you can directly get the image from the image URL.

https://i.ytimg.com/vi/<video_id>/default.jpg
https://i.ytimg.com/vi/<video_id>/mqdefault.jpg
https://i.ytimg.com/vi/<video_id>/hqdefault.jpg
https://i.ytimg.com/vi/<video_id>/sddefault.jpg
https://i.ytimg.com/vi/<video_id>/maxresdefault.jpg

If you want to get .webp version use the below code:

https://i.ytimg.com/vi_webp/<video_id>/default.webp
https://i.ytimg.com/vi_webp/<video_id>/mqdefault.webp
https://i.ytimg.com/vi_webp/<video_id>/hqdefault.webp
https://i.ytimg.com/vi_webp/<video_id>/sddefault.webp
https://i.ytimg.com/vi_webp/<video_id>/maxresdefault.webp

But keep in mind, all videos do not have .webp version thumbnail.

Method 3: Get YouTube Player Background Thumbnail

You can also get the YouTube player background thumbnails either .jpg or .webp format.

WebP
https://i.ytimg.com/vi_webp/<video_id>/0.webp

JPG
https://i.ytimg.com/vi/<video_id>/0.jpg

Method 4: Get video frames Thumbnail

To get the start, middle, and end frames of a video, use the below URL:

WebP:
Start: https://i.ytimg.com/vi_webp/<video_id>/1.webp
Middle: https://i.ytimg.com/vi_webp/<video_id>/2.webp
End: https://i.ytimg.com/vi_webp/<video_id>/3.webp

JPG:
Start: https://i.ytimg.com/vi/<video_id>/1.jpg
Middle: https://i.ytimg.com/vi/<video_id>/2.jpg
End: https://i.ytimg.com/vi/<video_id>/3.jpg

Replace <video_id> with your YouTube Video id.

Method 5: Get different resolution video Thumbnail

You can get the all different resolution video thumbnails from the below table. However, all YouTube video does not have this thumbnail because of video size.

WidthHeightURL
12090https://i.ytimg.com/vi/<video_id>/1.jpg
12090https://i.ytimg.com/vi/<video_id>/2.jpg
12090https://i.ytimg.com/vi/<video_id>/3.jpg
12090https://i.ytimg.com/vi/<video_id>/default.jpg
320180 https://i.ytimg.com/vi/<video_id>/mq1.jpg
320180 https://i.ytimg.com/vi/<video_id>/mq2.jpg
320180 https://i.ytimg.com/vi/<video_id>/mq3.jpg
320180 https://i.ytimg.com/vi/<video_id>/mqdefault.jpg
480360https://i.ytimg.com/vi/<video_id>/0.jpg
480360https://i.ytimg.com/vi/<video_id>/hq1.jpg
480360https://i.ytimg.com/vi/<video_id>/hq2.jpg
480360https://i.ytimg.com/vi/<video_id>/hq3.jpg
480360https://i.ytimg.com/vi/<video_id>/hqdefault.jpg
640480https://i.ytimg.com/vi/<video_id>/sd1.jpg
640480https://i.ytimg.com/vi/<video_id>/sd2.jpg
640480https://i.ytimg.com/vi/<video_id>/sd3.jpg
640480https://i.ytimg.com/vi/<video_id>/sddefault.jpg
1280720https://i.ytimg.com/vi/<video_id>/hq720.jpg
19201080https://i.ytimg.com/vi/<video_id>/maxresdefault.jpg

Conclusion

After reading this article, you should have a better understanding of how to get a YouTube video thumbnail from a URL in PHP. If there is anything that we can do for you about the topic or if you need help with something else related to marketing your business online, please feel free to contact us at any time.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment