Create thumbnails using Wordpress built-in functions

Filed under tech

I was pleased to discover that there’s a better way to create thumbnails in Wordpress than code it yourself, or use a third party library.

In fact, in wp-includes/media.php there’s a whole bunch of functions that Wordpress (since 2.5) uses to manipulate media files.

image_resize() is exactly what we need:

/**
 * Scale down an image to fit a particular size and save a new copy of the image.
 */
function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90)

You can use it like this to create a perfect 100×60 thumbnail:

// resize the image
$thumb = image_resize($img,100,60,true);

The tricky part is that, because Wordpress use this function in the admin, it needs some code that’s not available to themes by default.
You can fix this by including wp-admin/includes/image.php on top of your functions.php:

/// admin image API
require_once(ABSPATH . '/wp-admin/includes/image.php');

thumbnails.php

To make thumbnail generation even easier, I coded some usefull functions you can grab.
This functions work on images placed on posts and uploaded on wp-content.

Download thumbnails.php

the_thumb() is the easiest one. You can call it inside the Loop to extract the first image in the content, resize it, and output an image tag. Of course, like all Wordpress functions, there’s the corresponding get_the_thumb().

generate_thumb() it’s a shortcut for image_resize(), with some addictions.

Finally, extract_image() return the url of the first image in an html string.

// extract the first image of the post
$img = extract_image(get_the_content());

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

26 Comments

  1. Posted Wednesday October 14th, 2009 at 10:51 AM | Permalink

    I'm glad that someone else has found these functions.

    Here is a use-case for you:

    You have a WordPress blog with thousands of images. One day you change your template, and decide that all of your medium and thumnail images should be resized. But there is currently no way to do this.

    What someone needs to create is an image re-sizing plugin for WordPress that will resize images that have already been attached to posts.

    Cheers,
    Stuart.

  2. Posted Friday October 16th, 2009 at 08:08 PM | Permalink

    @Stuart

    That actually sounds like it would be a good idea.

    Oh and nice article

  3. Posted Friday October 16th, 2009 at 08:51 PM | Permalink

    The quick solution is to use full size images in your posts, and resize them on the template.
    So if you change the template and need bigger thumbnail, you just need to change width and height on the code.

    Cheers

  4. Check0
    Posted Sunday October 18th, 2009 at 02:52 PM | Permalink

    @Stuart: You can use the function mentioned in this blogpost ideally for that. Including old posts.

    Simply put them full-size in your posts and then make "thumbnail" (well, praticly it's not a thumbnail anymore, but simply a smaller-version of the image) by only setting the width to whatever you like.
    Then whenever the template or theme changes you only have to adjust the width and every page view of a blogpost will show the newly made thumbnails.

    I've been doing the above for a long time (using TimThumb), but now that I how to utitlize the built-in functions it will definitly consider droppping TimThumb for WP-Themes.


    Thanks fo the article !
    Grtz,
    C

  5. TeMC
    Posted Sunday October 18th, 2009 at 06:27 PM | Permalink

    #post author: How do I know actually use this ?

    I am getting :
    Catchable fatal error-: Object of class WP_Error could not be converted to string in * on line *

    I have this:<code>

    // get customfield
    $img = get_post_meta($post->ID, 'image', true);
    // resize the image
    $thumb = image_resize($img,100,60,true);
    // output
    ?><img src="<?php echo $thumb; ?>" alt="" /><?php

    </code>

  6. TeMc
    Posted Sunday October 18th, 2009 at 06:27 PM | Permalink

    #post author: How do I know actually use this ?

    I am getting :
    Catchable fatal error-: Object of class WP_Error could not be converted to string in * on line *

    I have this:<code>

    // get customfield
    $img = get_post_meta($post->ID, 'image', true);
    // resize the image
    $thumb = image_resize($img,100,60,true);
    // output
    ?>[img src="<?php echo $thumb; ?>" alt="" /]<?php

    ( [] = <> )

  7. Posted Sunday October 18th, 2009 at 09:01 PM | Permalink

    this wordpress functions are built to work with images attached to posts, so you may need some fox to make them works on custom images

    also remember to add

    require_once(ABSPATH . '/wp-admin/includes/image.php');

  8. Posted Sunday October 18th, 2009 at 11:40 PM | Permalink

    This is a great article and works well for pulling images out of posts but I'm in the same boat as TeMc. I'm trying to resize images being pulled from a custom field which doesn't work unfortunately. I'm also using TimThumb but wanted to replace it for the native WordPress functions.

    ~David
    http://wpclassipress.com

  9. Posted Wednesday October 21st, 2009 at 12:52 PM | Permalink

    Great way to handle dynamicly resize images. Thanx for the insight.

  10. TeMc
    Posted Thursday November 12th, 2009 at 09:15 AM | Permalink

    I had added the require-code, but that didn't do it.

    It's not really a custom image (as in; not external) but I like to keep the image link (which starts with /wp-content) in a custom field, becuase I display it out of the posts content, and also do other stuff with it.

    Also, "attached to post" isn't what I need, since sometimes I've uploaded an image before and wanna re-use it. In order to make it connect to this post I'd have to re-upload it, not very nice.

    Or I might upload the image(s) before I even write the article for whatever reason.

    Untill I can figure out how to use your function with a custom address, rather then the first one in the post content or the one asociated via the upload function.., I'd have to stick with TimThumb.

  11. Posted Friday December 11th, 2009 at 08:25 PM | Permalink

    Hey Valentino,
    I just managed to hack your code to make it work with the latest WordPress MU. Most of the work was fixing image paths for multiple blogs, other than that it's been pretty easy.
    I'll email you the WPMU compatible code over the weekend.

    Take care!

  12. Posted Monday December 14th, 2009 at 06:46 PM | Permalink

    awesome! :)

  13. ProWebDeveloper
    Posted Sunday May 23rd, 2010 at 04:49 PM | Permalink

    That's a terrible idea. You are requiring your visitors to download full-size images when only a small thumbnail is required. You see amateurs do this all of the time. They'll put up a 4MB image of something, then resize it via html to 100×100px. If they had resized the image properly (using a graphic editor), the image would be all of 10-15KB.

    Don't punish your users for your own laziness!

  14. Posted Monday September 20th, 2010 at 06:08 PM | Permalink

    Totally wrong: proper image resizers resize the image so that they are still 10-15KB but without requiring users to know how to use graphics programs. That the point in using the functions listed above. For professional themes, these kinds of functions are a godsend.

  15. Posted Tuesday September 21st, 2010 at 06:57 AM | Permalink

    Did this code ever get sent? Would love to see it!

  16. Posted Tuesday September 21st, 2010 at 06:45 PM | Permalink

    I've just emailed my code for WPMU to Valentino, so he can share it here.

  17. Posted Tuesday September 28th, 2010 at 09:00 AM | Permalink

    can i use this to make additional thumb for my theme

  18. Posted Saturday December 11th, 2010 at 05:32 AM | Permalink

    This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent wor

  19. Posted Thursday January 6th, 2011 at 09:23 AM | Permalink

    It is a good way to create thumbnails. I used it. I saw that it worked well.

  20. Posted Saturday January 22nd, 2011 at 06:30 AM | Permalink

    What someone needs to create is an image re-sizing plugin for WordPress that will resize images that have already been attached to posts.
    Processor comparison

  21. Posted Thursday February 17th, 2011 at 11:55 AM | Permalink

    I really admire your for this post.

  22. Posted Wednesday August 3rd, 2011 at 07:47 PM | Permalink

    Thanks! I didn't know about image_resize function before.

  23. delluk125
    Posted Monday August 29th, 2011 at 02:20 AM | Permalink

    I appreciate the helpful information. Personally, I always leave a useful comment. I get plenty of useless, spammy comments on my own site that I have to get rid of.

  24. hrbrt
    Posted Tuesday November 1st, 2011 at 01:56 PM | Permalink

    I cannot find the MU compatible code here. Would it be possible for you to share it here? Thanks

  25. madi
    Posted Saturday November 19th, 2011 at 10:00 PM | Permalink

    thanks for efforts I searched for similar solution 2 months ago and this one is perfect

  26. Posted Thursday December 1st, 2011 at 11:38 PM | Permalink

    I'd like it too – having issues with thumbnail URLs.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*