Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Retrieve 1st image in post and set it as featured image, when post is saved/updated
VT-resize image and display it

ok guys, I have this script which I modified in order to crop the image before being displayed.

// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}

 $first_img = vt_resize( $first_img, '', 608, 250, true );


return $first_img;
}

this is the code I use to display it:

<img src="<?php echo catch_that_image() ?>" width="608" height="250" />

The vt_resize function is already built in within the theme. However the code is not returning the url, it just return an array. What am I doing wrong?

I hope a nice person answers to my question, thank you =)

share|improve this question

marked as duplicate by Wyck, Rarst Apr 28 '12 at 8:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
FAQ > Etiquette. Your first and last warning. –  Rarst Apr 28 '12 at 8:08

2 Answers 2

Sorry to not help with your existing code but if you are open to using a plugin I can recommend: http://wordpress.org/extend/plugins/get-the-image/

It is great as it grabs the first image, or a featured image if you have one. You can also specify the dimensions to crop the image and an optional fallback image in case there is no image in the post.

You can just use <?php get_the_image(); ?> in a template and it will default to:

$defaults = array(
    'meta_key' => array( 'Thumbnail', 'thumbnail' ),
    'post_id' => $post->ID,
    'attachment' => true,
    'the_post_thumbnail' => true,
    'size' => 'thumbnail',
    'default_image' => false,
    'order_of_image' => 1,
    'link_to_post' => true,
    'image_class' => false,
    'image_scan' => false,
    'width' => false,
    'height' => false,
    'format' => 'img',
    'meta_key_save' => false,
    'callback' => null,
    'cache' => true,
    'echo' => true
);
share|improve this answer
    
Excellent! Thank you. One more question... Will this consume too much server resources? Because I have thousands of visits daily. I cannot use timthumb for that matter because it is resource intensive. Is this plugin better? –  Gixty Apr 27 '12 at 22:19
    
From experience, yes much better! I too gave up on using timthumb. But I'm not techie enough to talk specifics. Maybe take a look at: wordpress.org/extend/plugins/get-the-image/faq –  Caroline Elisa Apr 27 '12 at 22:27
    
Caroline for some reason the plugin does not crop the images :( –  Gixty Apr 27 '12 at 22:33
    
Have you tried selecting 'Crop thumbnail to exact dimensions (normally thumbnails are proportional)' under Media Settings? –  Caroline Elisa Apr 27 '12 at 22:37
    
I am not using thumbnails, I am extracting the first image of a post so there are many different sizes. I have tried many plugins and no luck! –  Gixty Apr 27 '12 at 22:44

This plugin from the Wordpress Plugin Directory seems to do what you need - Auto post thumbnail

share|improve this answer
    
hi travis, thank you but I already tried it out and it was giving me a bunch of errors so I had to deactivate it. –  Gixty Apr 27 '12 at 22:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.