Grabbing thumbnail image from youtube using php code

in PHP Code / 6 Comments

The following code is to grabbing the thumbnail image from youtube.

<?
/**
* Function: youtube thumb grabber
*
* @description :
* @param $ : video code, url type (embed/url),size (small/Large),thumb link
* @return : thumb path
*/
if (! function_exists(‘youtube_thumb_grabber’ ))
{

function youtube_thumb_grabber($video_code, $link_type = “embed”, $size = “small”, $thumb_link = “”)
{
if ($video_code != ”)
{
if ($link_type == “embed”)
{

$splited_data = explode(“=”,$video_code);

$video_unique_code = substr(strrchr($splited_data[4],”/”),1,-strlen(strrchr($splited_data[4],”&”)));

}
else if ($link_type == “url”)
{
$splited_data = explode(“=”,$video_code);
$video_unique_code = substr($splited_data[1],0,-strlen(strrchr($splited_data[1],”&”)));
}
else
{
return;
}

if($size == “small”)
{
return “<a href=\”$thumb_link\”><img src=\”http://img.youtube.com/vi/$video_unique_code/2.jpg\” alt=\”No image\” /></a>”;
}
else if ($size == “large”)
{
return “<a href=\”$thumb_link\”><img src=\”http://img.youtube.com/vi/$video_unique_code/0.jpg\” alt=\”No image\” /></a>”;
}
else
{
return “<a href=\”$thumb_link\”><img src=\”http://img.youtube.com/vi/$video_unique_code/2.jpg\” alt=\”No image\” /></a>”;

}

}

}
}

$video_title_url=”http://www.youtube.com/watch?v=ed3Vd39USYQ”;
$video_code=”<object width=\”560\” height=\”340\”><param name=\”movie\” value=\”http://www.youtube.com/v/ed3Vd39USYQ&hl=en_US&fs=1&\”></param><param name=\”allowFullScreen\” value=\”true\”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/ed3Vd39USYQ&hl=en_US&fs=1&” type=\”application/x-shockwave-flash\” allowscriptaccess=\”always\” allowfullscreen=\”true\” width=\”560\” height=\”340\”></embed></object>”;

echo youtube_thumb_grabber($video_code,”embed”,”large”,$video_title_url);
?>

Hope that this will be useful.

With more than 8 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ...Contact : [email protected]

Discussion 6 Comments

  1. saeed

    Good work 🙂 Still one q ??? where u find $video_code ??? (The object)

    Only embed available !!!!!!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>