How to get Youtube Thumbnail Images

Written by Tony Lea on Jan 19th, 2011 Views Report Post

There is a very simple way to retrieve your youtube thumbnail images for a specific video. The simplest way is to go to the following URL:

http://img.youtube.com/vi/VIDEO_ID/#.jpg

Of course, you'll need to replace [VIDEO_ID] with your youtube video ID and you'll need to add which thumbnail number you would like to grab. (0, 1, 2, or 3). thumbnail 0 is the default full size thumbnail, so to grab the large youtube thumbnail for 'Charlie The Unicorn' you would go to:

http://img.youtube.com/vi/Q5im0Ssyyus/0.jpg

Which would give you the following image:

Now if you wanted to retrieve the image from a PHP function, which accepts either the Youtube URL or the Youtube embed code, you could simply use the following function:

function get_youtube_video_image($youtube_code)
{
	// get the video code if this is an embed code	(old embed)
	preg_match('/youtube\.com\/v\/([\w\-]+)/', $youtube_code, $match);

	// if old embed returned an empty ID, try capuring the ID from the new iframe embed
	if($match[1] == '')
		preg_match('/youtube\.com\/embed\/([\w\-]+)/', $youtube_code, $match);
	
	// if it is not an embed code, get the video code from the youtube URL	
	if($match[1] == '')
		preg_match('/v\=(.+)&/',$youtube_code ,$match);

	// get the corresponding thumbnail images	
	$full_size_thumbnail_image = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
	$small_thumbnail_image1 = "http://img.youtube.com/vi/".$match[1]."/1.jpg";
	$small_thumbnail_image2 = "http://img.youtube.com/vi/".$match[1]."/2.jpg";
	$small_thumbnail_image3 = "http://img.youtube.com/vi/".$match[1]."/3.jpg";

	// return whichever thumbnail image you would like to retrieve
	return $full_size_thumbnail_image;		
}

That's it all you need to do is to pass in the youtube embed code or URL into the function and you will receive the video thumbnail.

You can view a demo of this functionality at the Youtube Image Thumbnail Generator:

Youtube Image Thumbnail Generator

With the Thumbnail generator you can enter the youtube embed code or youtube URL into the textarea and press the 'Get Images' button and all the corresponding thumbnails and their locations will be displayed.

Comments (0)