I put this code in a comment but wanted to share a better version here.

This is a code which needs to be modified and put within your code requirements. I created this code because the site was already using embedded youtube files but needed to show thumbnails as a teaser, efficiently.

First you need to install imagecache module to be able to resize the image realtime.

You can put this code in a theme function where you like to show the thumbnails and call this function within your .php.tpl file, if not already done.
$node variable needs to be as a reference to the function.

Considering the first array for the CCK content field name is field_embedded_link, you can change this by checking your field name. You can also uncomment print_r($field) and see the array.

  $title_size = 45;
  $field = get_object_vars($node);
  $matches = array();
  $match = '';
  //print_r($field); // you can uncomment this part to show the fields
	if (preg_match("/youtube/i", $field['field_embedded_link'][0]['value'])) { //
	preg_match('/(http:\/\/www.youtube.com\/v\/)(.+)(\"><\/param><param)/', $field['field_embedded_link'][0]['value'], $matches);
	$match = trim($matches['2']);
	$fieldthumb = 'files/youtube/t' . strtolower($match) . '.jpg';
		if (file_exists($fieldthumb)) { //if the file was already downloaded just output.
			$output = theme('imagecache','patternname',$fieldthumb); // show the thumbnail if already exists (check your imagecache settings and create a patternname according to your needs)
		} 
		else {
			$youtube_thumb = "http://img.youtube.com/vi/".$match."/default.jpg"; //retrieve and copy the image
			if (copy($youtube_thumb, $fieldthumb)) { //copy the file from the server and if does not fail continue
			chmod($fieldthumb, 0775); //maybe a lower more secure value.
			$output = theme('imagecache','patternname',$fieldthumb); // show the thumbnail
			}
			elseif (!file_exists($fieldthumb)) { // a patch for immediate http problems and avoid unnecessary loops.
					if (copy($youtube_thumb, $fieldthumb)){ //retry only once again if fails to be safe due to http transfer errors
					chmod($fieldthumb, 0775); //maybe a lower more secure value.
					$output = theme('imagecache','patternname',$fieldthumb); // show the thumbnail
					} 
					else { //do not show the placeholder if the file does not exist
					$output = 'something else';
					}
			}
		}
	}
	else {
	$output = 'something else';
	}
  $output .= 'modify or add something else to your code';
 
  return $output;
  }
}

Hope this is helpful. Please let me know your comments.

Cheers,

Mark Ercil

Comments

Anonymous’s picture

You can remove the $title_size, forgot to remove from the actual code.

Add:

	$match_e = explode('&', $match);
	$match = $match_e[0];

before:

$match = trim($matches['2']);

And add the real path up to files folder on for accurate implementation:

if (file_exists('/var/www/html/'.$fieldthumb))

Mark Ercil