So, if you are a web developer of some kind, you are probably at some point wondering how to retrieve the youtube video thumbnail…
Getting the youtube video is easy enough, thanks to the embed code capabilities nowadays. But, how do we nicely get the thumbnail of the youtube video along with the embed code?
Well, its actually extremely easy. All you need to remember is:
http://img.youtube.com/vi/YOUTUBE_VIDEO_ID/default.jpg
So how do we use the above to retrieve any youtube video thumbnail? Easy, just replace YOUTUBE_VIDEO_ID with the actual youtube video ID associated with the video you are embedding, and this will give you a thumbnail sized youtube image.
Now you are probably wondering how to get the Youtube Video ID from an embed code, eg:
< o b j e c t width=”425″ height=”344″><param name=”movie” value=”http://www.youtube.com/v/sypXY3TkNfA&hl=en&fs=1″></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/sypXY3TkNfA&hl=en&fs=1″ type=”application/x-shockwave-flash” allowscriptaccess=”always” allowfullscreen=”true” width=”425″ height=”344″></embed>< / o b j e c t >
and get: sypXY3TkNfA which is the video ID?
Extremely simple in PHP. Simply execute the code below:
preg_match(‘/youtube\.com\/v\/([\w\-]+)/’, $embed_code, $match);
And in the $match[1] variable, there would be the Youtube Video Id.
It is generally not recommended to access the youtube’s default.jpg everytime you try to display a video thumbnail. It is nearly just as easy to use PHP’s Curl to download/retrieve the image from youtube’s server and store a copy on your local server, something like this would do nicely:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, THE_YOUTUBE_THUMBNAIL_IMAGE_URL);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$fileContents = curl_exec($ch);
curl_close($ch);
$newImg = imagecreatefromstring($fileContents);
imagejpeg($newImg, ‘PATH_OF/WHERE_YOU/WANT_TO/STORE_THE/image.jpg’, 70);
Hope someone finds this information useful, as I have found it extremely useful myself!
Note: I don’t take any credit for the information above as I also found the information through Google, I am posting to share my experiences only!

Powerball Record: 13,014 RPM
March 20, 2009 at 9:13 pm
I just want to know where we can find those php code?
Is it on function.php? if it is where should we put code listed above?
Is it valid with all video theme or only for Certain theme?
Still got problem to add video thumbnail automatically