When you’re building social applications it’s always a good idea to include context relevant to certain information. This helps tie the experience together and makes things a lot nicer for the user. I find myself working on projects that need some sort of relevant screen shot of either the site or video someones linked to. This can be really nice for your users and is pretty simple to do.

I use a pretty standard/straightforward PHP script to get the screen shot from a Youtube or Vimeo video URL. The below is stripped down version which you can use in any of your applications.

The Code

function video_image($url){
	$image_url = parse_url($url);
	if($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com'){
		$array = explode("&", $image_url['query']);
		return "http://img.youtube.com/vi/".substr($array[0], 2)."/0.jpg";
	} else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){
		$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".substr($image_url['path'], 1).".php"));
		return $hash[0]["thumbnail_small"];
	}
}

You’ll notice that I try to determine whether the url passed to the function is either a Youtube or Vimeo link and do the proper calculations for either. You should probably add in a fallback, specific to your use case, if neither Vimeo or Youtube are found within the link passed.

For Youtube

You can change the following line of code in my script to return a different size of Youtube image:

return "http://img.youtube.com/vi/".substr($array[0], 2)."/0.jpg";

There are variations of the thumbnails available based on the thumbs set by the user when uploading on Youtube. You can change the number to get a different screenshot. Incrementing the number on the end of the image will show you the next thumbnail available:

return "http://img.youtube.com/vi/".substr($array[0], 2)."/default.jpg"; // Small Default
return "http://img.youtube.com/vi/".substr($array[0], 2)."/0.jpg"; // Large Default
return "http://img.youtube.com/vi/".substr($array[0], 2)."/1.jpg";
return "http://img.youtube.com/vi/".substr($array[0], 2)."/2.jpg";
return "http://img.youtube.com/vi/".substr($array[0], 2)."/3.jpg";

For Vimeo

You can change the following line of code in my script to return a different size of Vimeo image:

return $hash[0]["thumbnail_small"];

The variations available are:

return $hash[0]["thumbnail_small"];
return $hash[0]["thumbnail_medium"];
return $hash[0]["thumbnail_large"];

Conclusion

The Vimeo & Youtube API’s have a lot more information that you can pull including users avatars and so on. Hopefully this little script will help bring some nice context to your users instead of a dull static link.

20 Comments

todd patt Posted: May 18, 2011 @ 11:25pm

Is there someway to do this for screenshots of homepages?

ptamaro Posted: Nov 07, 2011 @ 12:42pm

…helpful information — nice and simple, works great, thanks.

Marc Brooks Posted: Nov 22, 2011 @ 6:22pm

You can also see urls from YouTube like youtu.be and that case, the video id is immediately after the host.

Bernd Posted: Dec 06, 2011 @ 2:56am

Hi Darcy,

here is the code for youtu.be links.

//code
function video_image($url){
$image_url = parse_url($url);
if($image_url['host'] == ‘www.youtube.com’ || $image_url['host'] == ‘youtube.com’){
$array = explode(“&”, $image_url['query']);
return “http://img.youtube.com/vi/”.substr($array[0], 2).”/default.jpg”;
} else if($image_url['host'] == ‘www.youtu.be’ || $image_url['host'] == ‘youtu.be’){
$array = explode(“/”, $image_url['path']);
return “http://img.youtube.com/vi/”.$array[1].”/default.jpg”;
} else if($image_url['host'] == ‘www.vimeo.com’ || $image_url['host'] == ‘vimeo.com’){
$hash = unserialize(file_get_contents(“http://vimeo.com/api/v2/video/”.substr($image_url['path'], 1).”.php”));
return $hash[0]["thumbnail_small"];
}
}
//code

B

Michael Caputo Posted: Jan 27, 2012 @ 10:46am

Thanks for this! Really a great help!
Wish the Youtube images were a bit larger though!

Alfonso Posted: Apr 13, 2012 @ 1:44pm

Hey man thanks for info
in my case, to get the image, on C# i had to put the php on a WWW, then get the text, and then start to split the text to get the image, not very efficient but it works

regards from mexico

Jusnit Posted: Apr 26, 2012 @ 3:45am

It will be easy get youtube images with above code. But the images will be particular size here, can we get images with our dimensions,
i.e, like 4:3, 3:4 etc… from youtube .

Rj Posted: May 16, 2012 @ 4:51am

can i get above code for asp.net-mvc3.
in my asp.net-mvc3 project need image file of any video url like youtube, vimeo, hulu video url etc.

chen Posted: Jul 14, 2012 @ 5:18pm

Thank you so much.. Really useful!!! I am breaking my head to capture screenshot of youtube video. I didnt know that <http://img.youtube.com/vi//default.jpg gives the image of any video. Thanks!!

Anderson Tagata Posted: Sep 18, 2012 @ 1:23am

This approach use cURL as secure alternative for vimeo in shared servers.

/**
* Basic cURL wrapper function for PHP
* @link http://snipplr.com/view/51161/basic-curl-wrapper-function-for-php/
* @param string $url URL to fetch
* @param array $curlopt Array of options for curl_setopt_array
* @return string
*/
function file_get_contents_curl($url, $curlopt = array()){
$ch = curl_init();
$default_curlopt = array(
CURLOPT_TIMEOUT => 2,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_USERAGENT => “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1″
);
$curlopt = array(CURLOPT_URL => $url) + $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch);
if($response === false)
trigger_error(curl_error($ch));
curl_close($ch);
return $response;
}

function video_image($url){
$image_url = parse_url($url);
if($image_url['host'] == ‘www.youtube.com’ || $image_url['host'] == ‘youtube.com’)
{
$array = explode(“&”, $image_url['query']);
return “http://img.youtube.com/vi/”.substr($array[0], 2).”/0.jpg”;
}
else if($image_url['host'] == ‘www.youtu.be’ || $image_url['host'] == ‘youtu.be’)
{
$array = explode(“/”, $image_url['path']);
return “http://img.youtube.com/vi/”.$array[1].”/default.jpg”;
}
else if($image_url['host'] == ‘www.vimeo.com’ || $image_url['host'] == ‘vimeo.com’)
{
//$hash = unserialize(file_get_contents(“http://vimeo.com/api/v2/video/”.substr($image_url['path'], 1).”.php”));
$hash = unserialize(file_get_contents_curl(“http://vimeo.com/api/v2/video/”.substr($image_url['path'], 1).”.php”));

return $hash[0]["thumbnail_small"];
}
}

godote Posted: Sep 20, 2012 @ 5:54am

added facebook video thumbnails

//code
function video_image($url){
$image_url = parse_url($url);
if($image_url['host'] == ‘www.youtube.com’ || $image_url['host'] == ‘youtube.com’){
$array = explode(“&”, $image_url['query']);
return “http://img.youtube.com/vi/”.substr($array[0], 2).”/default.jpg”;
} else if($image_url['host'] == ‘www.youtu.be’ || $image_url['host'] == ‘youtu.be’){
$array = explode(“/”, $image_url['path']);
return “http://img.youtube.com/vi/”.$array[1].”/default.jpg”;
} else if($image_url['host'] == ‘www.vimeo.com’ || $image_url['host'] == ‘vimeo.com’){
$hash = unserialize(file_get_contents(“http://vimeo.com/api/v2/video/”.substr($image_url['path'], 1).”.php”));
return $hash[0]["thumbnail_large"];
} else if($image_url['host'] == ‘www.facebook.com’ || $image_url['host'] == ‘facebook.com’){
return “https://graph.facebook.com”.substr($image_url['path'], 2).”/picture”;
}
}
//code

Stenli Posted: Sep 30, 2012 @ 5:04am

Fixing suggest because v parameter can be in any position
Replace:
$array = explode(“&”, $image_url['query']);
return “http://img.youtube.com/vi/”.substr($array[0], 2).”/0.jpg”;
With:
parse_str($image_url['query'], $array);
return “http://img.youtube.com/vi/”.$url_params['v'].”/0.jpg”;

David Needham Posted: Nov 08, 2012 @ 6:35pm

FWIW, I wrote another function to pull the title (with the help of this post and http://stackoverflow.com/questions/1216029/get-title-from-youtube-videos/13072063#13072063):

function video_get_title($url){
$image_url = parse_url($url);
if($image_url['host'] == ‘www.youtube.com’ || $image_url['host'] == ‘youtube.com’){
$array = explode(“&”, $image_url['query']);

// This is the youtube video code
$code = substr($array[0], 2);
// Get video feed info (xml) from youtube, but only the title | http://php.net/manual/en/function.file-get-contents.php
$video_feed = file_get_contents(“http://gdata.youtube.com/feeds/api/videos?v=2&q=”.$code.”&max-results=1&fields=entry(title)&prettyprint=true”);
// xml to object | http://php.net/manual/en/function.simplexml-load-string.php
$video_obj = simplexml_load_string($video_feed);
// Get the title string to a variable
$video_str = $video_obj->entry->title;
// Change the type to string
$title = (string) $video_str;
// Output
return $title;
}
}

Patrick Posted: Nov 09, 2012 @ 10:02am

Thanks all – you’re life (and time) savers!

toni michel Posted: Jan 20, 2013 @ 2:05pm

hi there, how can i adapt this code so they work with the video embed url? (not the video page itself)

Jeffrey Posted: Feb 16, 2013 @ 7:37pm

Cool blog! Is your theme custom made or did you
download it from somewhere? A theme like yours with a few
simple adjustements would really make my blog jump out.
Please let me know where you got your design.
Thank you

Clive Moore Posted: Jun 11, 2013 @ 7:10am

Just been trying to get this to work on a VIMEO and a YouTube video, tried all options, none work..
Unless I am inputting the wrong URL format, the code is DEAD!!!!

Is this to do with updated APIs’ including vimeo switching to https://???

hope you have an answer, or even a tip what I am doing wrong??

Clive Moore Posted: Jun 11, 2013 @ 8:08am

UPDATE::
Seems that parse_url($url)
has a bug concerning its output with none http:// [scheme]
if you use a

http://www.vimeo.com/1234567
the array output is::

[path] => http://www.vimeo.com/1234567 – this does not work!

whereas:: using http://www.vimeo.com/1234567
gives the following array::

[scheme] => http
[host] => http://www.vimeo.com
[path] => /1234567

This version WORKS…

Clive Moore Posted: Jun 11, 2013 @ 9:52am

UPDATE::
Seems after several hours of trying to get this to work, its all down to not being able to pass a global $url to the function – in the end GAVE up! Split all the code out of the FUNCTION and used it without the need of a function. using if and else….

is this a php insider joke???

Works just as well, but would like an answer to why a $url would not pass to the ::

function video_thumbnail($url){
global $url;
$image_url = parse_url($url);

Juan Pablo Garcia Posted: Aug 14, 2013 @ 6:53pm

Just created a jQuery plugin

https://github.com/Ideame/jquery-vimeothumb

hope it helps!

Leave a Reply: