Grabbing thumbnail image from youtube using php code

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.

Anil Kumar Panigrahi

With more than 10 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]

You may also like...

6 Responses

  1. anbazhagan says:

    HI

    super working fine after some modification

  2. igaten says:

    nice code… thanks

  3. kuldeep says:

    nice trick but not professional

  4. John says:

    Nice information ; thank you for sharing . . .

  5. saeed says:

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

    Only embed available !!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.