How do I get a YouTube video thumbnail from the YouTube API?

  • Added:
  • |
  • In: Basic PHP

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?

This Question Has 23 Answeres | Orginal Question | Ryan

In YouTube API V3 we can also use these URLs for obtaining thumbnails... They are classified based on their quality.<insert-youtube-video-id-here>/default.jpg -   default<insert-youtube-video-id-here>/mqdefault.jpg - medium<insert-youtube-video-id-here>/hqdefault.jpg - high<insert-youtube-video-id-here>/sddefault.jpg - standard

And for the maximum resolution..<insert-youtube-video-id-here>/maxresdefault.jpg

One advantage of these URLs over the URLs in the first answer is that these URLs don't get blocked by firewalls.

YouTube is owned by Google and Google likes to have a reasonable number of images for different screen sizes hence its images are stored in different sizes, here is an example of how your thumbnail like will he like

Low quality thumbnail:<YouTube_Video_ID_HERE>/sddefault.jpg

Medium quality thumbnail:<YouTube_Video_ID_HERE>/mqdefault.jpg

High quality thumbnail:<YouTube_Video_ID_HERE>/hqdefault.jpg

Maximum quality thumbnail:<YouTube_Video_ID_HERE>/maxresdefault.jpg

What Asaph said is right. However, not every YouTube video contains all nine thumbnails, but there are seven thumbnails for every video. They are:

(Image size depends on video.)

  • Player Background Thumbnail (480x360 pixels)

  • Start Thumbnail (120x90 pixels)

  • Middle Thumbnail (120x90 pixels)

  • End Thumbnail (120x90 pixels)

  • High Quality Thumbnail (480x360 pixels)

  • Medium Quality Thumbnail (320x180 pixels)

  • Normal Quality Thumbnail (120x90 pixels)

And additionally, the next two thumbnails may or may not exist. For HQ videos they exist.

  • Standard Definition Thumbnail (640x480 pixels)

  • Maximum Resolution Thumbnail (1920x1080 pixels)

You can get the JavaScript and PHP scripts to retrieve thumbnails and other YouTube information in

Also use the tool YouTube Video Information Generator to get all the information of a YouTube video by submitting aURL or video ID.

Save file as .js

var maxVideos = 5;
      part: 'snippet,contentDetails',
      kind: 'youtube#videoListResponse',
      maxResults: maxVideos,
      regionCode: 'IN',
      key: 'Your_API_KEY'},
        var output;
        $.each(data.items, function(i, item){
                thumb = item.snippet.thumbnails.high.url;
          output = '<div id="img"><img src="' + thumb + '"></div>';
<!DOCTYPE html>
<div class="main">
 <ul id="thumbnail"> </ul>

I found this nifty tool that allows you to create the image with the YouTube play button placed over the image:

I have used YouTube Thumbnails in this way:

$url = '' . $youtubeId . '/0.jpg';
$img = dirname(__FILE__) . '/youtubeThumbnail_'  . $youtubeId . '.jpg';
file_put_contents($img, file_get_contents($url));

Remember YouTube prevents to include Images directly from their server

Just to add / expand on the solutions given, I feel it is necessary to note that, as I had this problem myself, one can actually grab multiple YouTube videos content, in this case thumbnails, with one HTTP request:

Using a Rest Client, in this case HTTPFUL, you can do something like this:

header("Content-type", "application/json");

//download the httpfull.phar file from

$youtubeVidIds= array("nL-rk4bgJWU", "__kupr7KQos", "UCSynl4WbLQ", "joPjqEGJGqU", "PBwEBjX3D3Q");

$response = \Httpful\Request::get("".implode (",",$youtubeVidIds)."")


print ($response);


I think their are a lots of answer for thumbnail, But I want to add some other urls to get youtube thumbnail very easily. I am just taking some text from Asaph's answer. Here are some url to get youtube thumbnails<insert-youtube-video-id-here>/default.jpg

For the high quality version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a url similar to the HQ:<insert-youtube-video-id-here>/mqdefault.jpg

For the standard definition version of the thumbnail, use a url similar to this:<insert-youtube-video-id-here>/sddefault.jpg

For the maximum resolution version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/maxresdefault.jpg

Hope it helps someone in near future.

You can get the Video Entry which contains the URL to the video's thumbnail. There's example code in the link. Or, if you want to parse XML, there's information here. The XML returned has a media:thumbnail element, which contains the thumbnail's URL.

If you want to get rid of the "black bars" and do it like YouTube does it, you can use :<video id>/mqdefault.webp

And if you can't use .webp extension you can do it like this :<video id>/mqdefault.jpg

Also, if you need the unscaled version use maxresdefault instead of mqdefault.

Note: I'm not sure about the aspect ratio if you're planning to use maxresdefault.


If all you want to do is search YouTube and get associated properties:

  1. Get a PUBLIC API -- This Link gives good direction

2.Use below query string. The search query (denoted by q=) in the url string is stackoverflow for example purposes. YouTube will then send you back a json reply where you can then parse for Thumbnail, Snippet, Author, etc.

You can use YouTube Data API to retrieve video thumbnails, caption, description, rating, statistics and more. API version 3 requires a key*. Obtain the key and create a videos: list request:

Example PHP Code

$data = file_get_contents("");
$json = json_decode($data);


object(stdClass)#5 (5) {
  object(stdClass)#6 (3) {
    string(46) ""
  object(stdClass)#7 (3) {
    string(48) ""
  object(stdClass)#8 (3) {
    string(48) ""
  object(stdClass)#9 (3) {
    string(48) ""
  object(stdClass)#10 (3) {
    string(52) ""

* Not only that you need a key, you might be asked for billing information depending on the number of API requests you plan to make. However, few million requests per day are free.

Source article.

// Get image form video URL
$url = $video['video_url'];

$urls = parse_url($url);

//Expect the URL to be, where abcd is the video ID
if ($urls['host'] == '') :

    $imgPath = ltrim($urls['path'],'/');

//Expect the URL to be
elseif (strpos($urls['path'],'embed') == 1) :

    $imgPath = end(explode('/',$urls['path']));

//Expect the URL to be abcd only
elseif (strpos($url,'/') === false):

    $imgPath = $url;

//Expect the URL to be
else :


    $imgPath = $v;


Use:,id&maxResults=100&regionCode=us&key=**Your YouTube ID**

Above is the link. Using that, you can find the YouTube characteristics of videos. After finding characteristics, you can get videos of the selected category. After then you can find selected video images using Asaph's answer.

Try the above approach and you can parse everything from the YouTube API.

Another good alternative would be to use the oEmbed API which is supported by YouTube.

You simply add your YouTube Url to the oEmbed url and you'll receive a JSON including a thumbnail and the html code for embedding.


Would give you:

  thumbnail_url: "",
  width: 459,
  author_name: "AllKindsOfStuff",
  version: "1.0",
  author_url: "",
  thumbnail_width: 480,
  type: "video",
  provider_url: "",
  html: "<iframe width="459" height="344" src="" frameborder="0" allowfullscreen></iframe>",
  title: "Some title bla bla foo bar",
  thumbnail_height: 360,
  provider_name: "YouTube",
  height: 344

Read the Documentation for more Information.

If you want the biggest image from YouTube for a specific video ID, then the URL should be something like this:

Using the API, you can pick up default thumbnail image. Simple code should be something like this:

//Grab the default thumbnail image
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
$thumbnail = substr($thumbnail, 0, -5);
$thumb1 = $thumbnail."default.jpg";

// Grab the third thumbnail image
$thumb2 = $thumbnail."2.jpg";

// Grab the fourth thumbnail image.
$thumb3 = $thumbnail."3.jpg";

// Using simple cURL to save it your server.
// You can extend the cURL below if you want it as fancy, just like
// the rest of the folks here.

$ch = curl_init ("$thumb1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);

// Using fwrite to save the above
$fp = fopen("SomeLocationInReferenceToYourScript/AnyNameYouWant.jpg", 'w');

// Write the file
fwrite($fp, $rawdata);

// And then close it.

a simple php function i created for the youtube thumbnail and the types are

  • default
  • hqdefault
  • mqdefault
  • sddefault
  • maxresdefault

    function get_youtube_thumb($link,$type){
        $video_id = explode("?v=", $link); 
            if (empty($video_id[1])){
               $video_id = explode("/v/", $link); 
               $video_id = explode("&", $video_id[1]); 
               $video_id = $video_id[0];
        $thumb_link = "";
        if($type == 'default' || $type == 'hqdefault' || $type == 'mqdefault' || $type == 'sddefault' || $type == 'maxresdefault'){
            $thumb_link = ''.$video_id.'/'.$type.'.jpg';
        }elseif($type == "id"){
            $thumb_link = $video_id;
        return $thumb_link;}

If you're using the public api, the best way to do it is using if statements.

If the video is public or unlisted, you set the thumbnail using the url method. If the video is private you use the api to get the thumbnail.

if($video_status == 'unlisted'){
$video_thumbnail = ''.$video_url.'/mqdefault.jpg';
$video_status = '<i class="fa fa-lock"></i>&nbsp;Unlisted';
elseif($video_status == 'public'){
$video_thumbnail = ''.$video_url.'/mqdefault.jpg';
$video_status = '<i class="fa fa-eye"></i>&nbsp;Public';
elseif($video_status == 'private'){
$video_thumbnail = $playlistItem['snippet']['thumbnails']['maxres']['url'];
$video_status = '<i class="fa fa-lock"></i>&nbsp;Private';

In YouTube Data API v3, you can get video's thumbnails with the videos->list function. From snippet.thumbnails.(key), you can pick the default, medium or high resolution thumbnail, and get its width, height and URL.

You can also update thumbnails with the thumbnails->set functionality.

For examples, you can check out the YouTube API Samples project. (PHP ones.)

YouTube Data API

YouTube Provides us the 4 generated images for every video through the Data API (v3), For Ex -





Getting Access to the Images Via API

  1. First Get Your Public API Key at Google API Console.
  2. As Per YouTube's Thumbnail Reference in the API Documentation, You need to access the resources on snippet.thumbnails.
  3. As Per this, you need to phrase your URL like this -

Now Change yourVideoId and yourApiKey to the your respective video-id and api-key and its response will be a JSON output providing you the 4links in the thumbnails of snippet variable (If all are available).

I made a function to only fetch existing images from YouTube

function youtube_image($id) {
    $resolution = array (

    for ($x = 0; $x < sizeof($resolution); $x++) {
        $url = '//' . $id . '/' . $resolution[$x] . '.jpg';
        if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
    return $url;
public const string tubeThumb = "[id]/hqdefault.jpg";
vid.Thumbnail = tubeThumb.Replace("[id]", vid.VideoID);

Each YouTube video has 4 generated images. They are predictably formatted as follows:<insert-youtube-video-id-here>/0.jpg<insert-youtube-video-id-here>/1.jpg<insert-youtube-video-id-here>/2.jpg<insert-youtube-video-id-here>/3.jpg

The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:<insert-youtube-video-id-here>/default.jpg

For the high quality version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a url similar to the HQ:<insert-youtube-video-id-here>/mqdefault.jpg

For the standard definition version of the thumbnail, use a url similar to this:<insert-youtube-video-id-here>/sddefault.jpg

For the maximum resolution version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/maxresdefault.jpg

All of the above urls are available over http too. Additionally, the slightly shorter hostname works in place of in the example urls above.

Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.

Meet with owner

Sajjad Hossain

Hey, I am Sajjad, working in web development sector since 2012. I love to do amazing things. Let's do a project together.
Connect Social With PHPAns