Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function grab_that_image() {

######################################

// by Paul Burgess http://iampaulburgess.co.uk

// based on the original catch_that_image function found here:
// http://wordpress.org/support/topic/246893#post-1212902

// for this to work you need Tim Thumb already up and running:
// http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/

// and a custom field in your WP site called 'first_img'- which you set to say 'none' if you don't want an image to show 

// also upload a default image to use as a fall back to /wp-content/uploads/default.jpg

######################################

global $post, $posts;

//absolute path to timthumb file - with NO trailing slash
$path_to_thumb_file = "/wp-content/uploads";
$thumb_file_name = "timthumb.php";

// complete path
$default_image = "/wp-content/uploads/default.jpg";

  $first_img = '';
  ob_start();
  ob_end_clean();

// custom field for first image
$first_img =  get_post_meta($post->ID, "first_img", $single = true);

// if custom field is empty...
if(empty($first_img)){

// we'll try for the first image in post
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);

$first_img_found = $matches [1] [0];

/*
To allow Tim Thumb to work:
- Remove domain
- and Tim Thumb file path in case its already in use
*/

$first_img = str_replace(array("http://".$_SERVER['SERVER_NAME']."","".$path_to_thumb_file."/".$thumb_file_name."?src="),'',$first_img_found);

// If the image is hosted on another website, we can't auto generate the thumbnail - so we'll fall back to the default
// if $orig_img contains http:// and not the site server name - it's an external image
if(stristr($first_img_found,"http://") && !stristr($first_img_found,$_SERVER['SERVER_NAME'])){

// therefore $first_img is the default
$first_img = $default_image;

} // end if for checking if image is external

} // end if for checking if there's no custom field

// Include the option to have no image display via the custom field
// this goes back to the custom field - if it says 'none' then we won't show an image

if ($first_img == "none" || empty($first_img_found)){
$image_html = NULL;
} else {
$image_html = "<img src=\"".$path_to_thumb_file."/".$thumb_file_name."?src=$first_img&w=210&h=130&zc=1&q=90\" alt=\"".get_the_title($post->post_parent)."\" />";
 }

return $image_html;

}

?>