In video gallerys YouTube shows images of the videos instead of the flash player. If you click on the image, you are redirected to a page where is the flash video player. I want to show the first static images.
How can I do that programmatically?
For Javascript: (I'm assuming you tagged it as flash because Youtube is a flash video player)
function getScreen( url, size )
{
if(url === null){ return ""; }
size = (size === null) ? "big" : size;
var vid;
var results;
results = url.match("[\\?&]v=([^&#]*)");
vid = ( results === null ) ? url : results[1];
if(size == "small"){
return "http://img.youtube.com/vi/"+vid+"/2.jpg";
}else {
return "http://img.youtube.com/vi/"+vid+"/0.jpg";
}
}
Found here.
Going off of this function, basically you just have to take the v=ladlfasd parameter and put it in this url:
http://img.youtube.com/vi/(v= parameter)/2.jpg
Where it is 2.jpg for small, 0.jpg for large
function getYoutubeThumbnail($url)
{
if(preg_match('![?&]{1}v=([^&]+)!', $url . '&', $m))
{
$videoid = $m[1];
}
else if(preg_match('~/v/([0-9a-z_]+)~i', $url, $m))
{
$videoid = $m[1];
}
$youtube_thumbnail = 'http://img.youtube.com/vi/' . $videoid . '/default.jpg';
$c = curl_init();
$url = trim($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
$contents = curl_exec($c);
curl_close($c);
$feed = "http://gdata.youtube.com/feeds/api/videos/".$videoid;
$newInfo = trim(@file_get_contents($feed));
preg_match('/<media:title(.*?)<\/media:title>/', $newInfo, $result);
$title = strip_tags($result[0]);
preg_match('/<media:keywords(.*?)<\/media:keywords>/', $newInfo, $result);
$desc = strip_tags(str_replace(",", "", $result[0]));
//embed path
$embed_path = "http://www.youtube.com/embed/".$videoid;
$youtube_info = array('videoid' => $videoid,'title' => $title, 'description' => $desc,'youtube_thumbnail' => $youtube_thumbnail,'embed_path' => $embed_path) ;
return $youtube_info;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With