Getting thumbnail from YouTube

Getting thumbnail image of any YouTube video you need it’s video ID number. Video ID number is the 11 character string in the url and assigned to v parameter. For example video ID of this video http://www.youtube.com/watch?v=S5msjdZIoag&feature=related is S5msjdZIoag.

You can get the video ID by the URL simply with strpos() function in PHP.

function getVideoID($url) {
    // get v parameter position
    $pos = strpos($url,"?v=");
    if ($pos==false) $pos = strpos($url,"&v=");
 
    if ($pos==false) return false;
 
    return substr($url,$pos+3,11);
}

The url http://i.ytimg.com/vi/S5msjdZIoag/default.jpg is the thumbnail image of the video above. As you see the S5msjdZIoag string is present in this url too. So url of thumbnail image of any video is http://i.ytimg.com/vi/<video_id>/default.jpg

function getVideoID($url) {
    // get v parameter position
    $pos = strpos($url,"?v=");
    if ($pos==false) $pos = strpos($url,"&v=");
 
    if ($pos==false) return false;
 
    return substr($url,$pos+3,11);
}
 
function getVideoThumbnail($videoId) {
 
    return "http://i.ytimg.com/vi/".$videoId."/default.jpg";
 
}
 
$url = "http://www.youtube.com/watch?v=S5msjdZIoag&feature=related";
 
$videoId = getVideoID($url);
 
echo (getVideoThumbnail($videoId));

Reference:

http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html

Facebook comments:

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">