How To Make Timthumb Get Youtube and Vimeo Video Thumbnails, Add Default Thumbnail When No Images Are Present

Filed July 6th, 2010 under Tutorial and WordPress

One of our main portfolio pieces is a music blog called SalaciousSound that I started about 2 years back. I’ve been using timthumb on the site for about 6 months, though when I first added it I wasn’t able to grab thumbnails for videos, and it also didn’t handle posts without images well. I’ve added two pieces of functionality to the function that calls timthumb, catch_that_image(), which resides in my theme’s functions.php file. The former grabs thumbs from videos as the title describes, and the latter just adds a default image that I have defined if the function can not find an image or a video to get a thumbnail from.

function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if ( $first_img != '' ) {
return urlencode($first_img);
}
/*
$match = array();
preg_match("/http://www.youtube.com/v/([^"][a-zA-Z0-9-_]+)[&"]/siU",$post->post_content,$match);
if ( $match[1] != '' ) {
return 'http://img.youtube.com/vi/'.$match[1].'/0.jpg';
}
$vimmatch = array();
preg_match('@vimeo.com/[^"&d]*([^"&]+)@i', $post->post_content, $vimmatch);
if ($vimmatch[1] != '') {
$vimhash = unserialize (file_get_contents ("http://vimeo.com/api/v2/video/".$vimmatch[1].".php"));
return $vimhash[0]['thumbnail_medium'];
}
*/
else { return 'http://url.com/wp-content/themes/themefolder/images/defaultimage.png'; }

}
Cailen
Cailen lives in Toronto. He is passionate about music and coffee culture, photography and videography, and code efficiency.

2 Responses

§ LeShaun Johnson wrote on October, 12th:

I get an error on the whole blog that says:

“Parse error: syntax error, unexpected ‘&’ in /home/a5675455/public_html/blog/wp-content/themes/FreshMag/functions.php on line 409″

After replacing my catch_that_image() function with your rewritten function… And that was before I uncommented the added code.

§ Mark wrote on April, 4th:

Same error as the above poster. It doesn’t like the ‘&’ on this line:

$output = preg_match_all(‘//i’, $post->post_content, $matches);

Leave a Reply