How to get a YouTube Video thumbnail to Using PHP - Mostlikers

15 May, 2017

How to get a YouTube Video thumbnail to Using PHP

Hi Guys, Today we are going to see How to get a YouTube Video thumbnail to Using YouTube API. This is a simple script to get youtube videos thumbnail images. Google API key not necessary to get the thumbnail images, But using Youtube API you can get the more size of images.

How to get a YouTube Video thumbnail to Using PHP


Get Youtube video thumbnail without using API

Share the youtube video URL load $url = "" value. The regular expressions fetch the youtube video id. 
<?php
    $url ="https://www.youtube.com/watch?v=Q7H3_A0KmbU";
    $regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/";
    $match;
    if(preg_match($regex_pattern, $url, $match)) {
        $vidID = $match[4];
    }
    else {
        echo "Sorry, not a youtube URL";
    }
    echo '<h6> Small Images</h6>';
    echo "<img src='https://img.youtube.com/vi/".$vidID."/default.jpg' />";
    echo "<img src='https://img.youtube.com/vi/".$vidID."/1.jpg' />";
    echo "<img src='https://img.youtube.com/vi/".$vidID."/2.jpg' />";
    echo "<img src='https://img.youtube.com/vi/".$vidID."/3.jpg' />";
    echo '<h2> Defult Images</h2>'; 
    echo "<img src='https://img.youtube.com/vi/".$vidID."/0.jpg' />";
?>

The first one in the list is a full-size image. The default thumbnail image is:

https://img.youtube.com/vi/<video-id-here>/default.jpg

For the high-quality version of the thumbnail use a use a URL with:

https://img.youtube.com/vi/<video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a URL similar to the HQ:

https://img.youtube.com/vi/<video-id-here>/mqdefault.jpg

For the standard definition version of the thumbnail, use a URL with sddefault:

https://img.youtube.com/vi/<video-id-here>/sddefault.jpg

For the maximum resolution version of the thumbnail use a URL with maxresdefault:

https://img.youtube.com/vi/<video-id-here>/maxresdefault.jpg

In case you want to store the image use this :

$image = file_get_content('https://img.youtube.com/vi/<video-id-here>/default.jpg');

No comments:

Post a comment