Forum | Image thumbnails

You must be logged in to post Login

Image thumbnails

UserPost

2:45 pm
October 14, 2009


Chris Ullyott

Fullerton, CA

Member

posts 66

Hey everyone, 

Since we have been having trouble with the image resizer the Gazette theme, I'm wondering if anyone has had any success with a thumbnail plugin or anything like that. We'd like some images to be shown next to our search results and also in our RSS feeds.

Thanks!

Chris Ullyott | Daily Titan, CSU Fullerton | cullyott@dailytitan.com

11:29 pm
October 14, 2009


Mo Jangda

Toronto, ON

Member

posts 35

Here's a code snippet you could use. It's not perfect nor pretty but works (though, it could use some error checking and less hooking into admin functions and less usage of ABSPATH). Add to functions.php.

/*
 * Returns the first image attached to a post
 * @param $size One of 'thumbnail', 'medium', 'large', 'full', or array( width, height )
 * @param $crop Crop the image if true
 */
function get_first_image( $size='thumbnail', $crop = false ) {
    global $post;
   
    $images =& get_children( 'post_type=attachment&post_mime_type=image&numberposts=1&post_parent=' . $post->ID );
               
    if (!empty($images)) {

        $attachment_ID = array_shift(array_keys($images));
       
        if($crop) {
            // Include WordPress Image functions | needed to resize
            require_once(ABSPATH.'/wp-admin/includes/image.php');
       
            // Get the path for the image
            $img_path = get_attached_file($attachment_ID);
            // Crop & Resize the image
            $cropped = image_resize( $img_path, $size[0], $size[1], true);
           
            // Get the new image URL
            $attachment_url = wp_get_attachment_url($attachment_ID);
            $img = path_join(dirname($attachment_url), basename($cropped));
        } else {
            //We're not cropping, so we just get the image src directly
            $img = wp_get_attachment_image_src($attachment_ID, $size);
            $img = $img[0];
        }       
        return $img;
    }
    return null;
}

Then use it in your loop as such:

<?php $image = get_first_image('thumbnail'); ?>

<?php if($feature_image) : ?>

<a class="featured-image" href="<?php the_permalink() ?>" rel="bookmark" title="View <?php the_title(); ?>">

<img src="<?php echo $image ?>" alt="Image from <?php the_title() ?>" class="image" />

</a>

<?php endif; ?>


Or if you want a image of a different dimension (say 200, x 100 cropped), change the function to:

<?php $image = get_first_image(array(200, 100), true); ?>

11:34 pm
October 14, 2009


Mo Jangda

Toronto, ON

Member

posts 35

If you don't care much about the ability to get custom sizes or crop images:

/*
 * Returns the first image attached to a post
 * @param $size One of 'thumbnail', 'medium', 'large', 'full', or array( width, height )
 */
function get_first_image( $size='thumbnail' ) {
    global $post;
   
    $images =& get_children( 'post_type=attachment&post_mime_type=image&numberposts=1&post_parent=' . $post->ID );
               
    if (!empty($images)) {

        $attachment_ID = array_shift(array_keys($images));
       
        $img = wp_get_attachment_image_src($attachment_ID, $size);
        $img = $img[0];
              
        return $img;
    }
    return null;
}

12:27 am
October 16, 2009


William P. Davis

Veazie, Maine

Admin

posts 65

You should note: these methods will only work if the image is attached to the post, which only happens when you upload the image in the post.

wpdavis.com | Editor in Chief, The Maine Campus | Associate, CoPress | will@copress.org | 207.660.5342

9:53 pm
October 19, 2009


Mo Jangda

Toronto, ON

Member

posts 35

Ah, yeah, good point Will. This wouldn't work for images linked from other websites. Wouldn't work either if you reuse an image across multiple posts (though, the simple workaround for this is to re-upload the image for each post — but that has obvious disadvantages).


About the CoPress forum

Most Users Ever Online:

119


Currently Online:

7 Guests

Forum Stats:

Groups: 1

Forums: 7

Topics: 107

Posts: 538

Membership:

There are 151 Members

There have been 2 Guests

There are 5 Admins

There is 1 Moderator

Top Posters:

Chris Ullyott – 66

Mo Jangda – 35

arobinsonwku – 32

laurenmichell – 21

CMLife – 16

sbressler – 15

Administrators: Daniel Bachhuber (102 Posts), William P. Davis (65 Posts), joey (39 Posts), Greg Linch (14 Posts), adam (1 Post)

Moderators: Andrew Spittle (49 Posts)