Get the Youtube or Vimeo image still from the video URL with PHP

This is a handy function to retrieve the youtube of vimeo still from the video URL. To use the function you just need to pass the $url parameter to the function. The $size parameter is optional and will by default return the large image. If you pass “thumb” as size it will return a small still icon.  

<?php
function video_image($url,$size="large"){
	if($size=="thumb"){	
		$size=1;
	}else{
		$size=0;
	}

	$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)."/" . $size . ".jpg";
	} elseif($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"];
	}
}

 

2 thoughts on “Get the Youtube or Vimeo image still from the video URL with PHP

  1. Change line 15 to  return $hash[0][“thumbnail_large”]; if you need larger vimeo.It seems the parameter $size applies only to yt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s