PHP Thumbnail Image of Youtube Video

With PHP language, I can give you a tutorial how to catch / captive a current youtube image thumbnail from the certain video. From the example below, I show to you how to get the thumbnail of IRON MAN 2 UPDATE from Youtube.
The URL : http://www.youtube.com/watch?v=1TiYlG21VAQ.

cdl_capture_2011-07-14-46_ 000

<?

// we make a function, so it will easy to applicate

function getinput($url)

{

$regexp= "http:\/\/www\.youtube\.com\/watch\?v=(.*)(.*)";
if(preg_match_all("/$regexp/siu", $url, $matches))

return $matches[1][0];

}

// Catch Youtube Thumbnail Image from <b>IRON MAN 2</b> UPDATE MOVIE

$youtube_url = "http://www.youtube.com/watch?v=1TiYlG21VAQ";
$thumbnail_of_youtube = getinput($youtube_url);

// The thumbnail image of Youtube always has a pattern like this : http://i1.ytimg.com/vi/<b>[videoid]</b>/default.jpg

echo ‘<a href="$youtube_url"><img src="http://i1.ytimg.com/vi/’ . $thumbnail_of_youtube . ‘/default.jpg"></a>’;

?>

I implemented a new feature over at my Jenny McCarthy and Hayden Panettiere websites this past week.

I’ve been posting YouTube videos on both sites for quite some time, but have only been providing text descriptions to go along with the links from the main video pages. I finally decided that I wanted to figure out how to dynamically include thumbnail image previews of the clips, not only to display on the main video pages, but also in web feeds where applicable.

First I got all fancy and was sending in YouTube API requests to get at the information. The youtube.videos.get_details function provides thumbnail information in its <thumbnail_url> node.

However, once I had implemented a preliminary approach using that method, I quickly realized there was a very obvious pattern to the thumbnail URLs being returned with each call:

http://img.youtube.com/vi/LiIboq6XCOg/2.jpg

The only portion of the above URL that seems to change is the videoID between the fourth and fifth forward slashes.

So rather than calling the API multiple times to get thumbnails for all the videos presented on the page, I decided to keep all function calls local, opting to create my own internal function for extracting theYouTube video ID from my XHTML-compliant embed code using aregular expression and then inserting that between thehttp://img.youtube.com/vi/ and /2.jpg portions of the static thumbnail URL.

Leave a comment