New WordPress Plugin: Custom Image Sizes

I make a lot of WordPress themes, and frequently clients want to associate a particular size of image with a post. You can do this easily with WordPress by using add_image_size() to define an image and then by calling wp_get_attachment_image() later to print the markup for that image.

So for example, if I have an attachment image of ID number 123, I might do something like the following:


add_image_size( 'my-custom-size', 220, 180, true );
...
echo wp_get_attachment_image( 123, 'my-custom-size' );

Here, add_image_size() defines the custom thumbnail (in this example the arguments tell add_image_size() to make it 220 pixels wide, 180 high, and cropped), and

echo wp_get_attachment_image()

prints the markup of the image itself, <img> element and everything.

The Problem

This works great; WordPress even creates thumbnails in this size from now on. The problem is that it doesn’t apply to pre-existing thumbnails. And if people change their minds about what sizes they want their thumbnails to be (clients sometimes change their minds) you’re stuck with existing thumbnails of the wrong size.

In addition, if you call wp_get_attachment_image( 123, 'my-custom-size' ); and that size doesn’t exist, WordPress just scales down the larger-sized original image, which might cause some performance issues.

Solution: Custom Image Sizes

My solution is the Custom Image Sizes plugin. You activate it, and if you call wp_get_attachment_image() and related functions for an attachment that doesn’t have that size, WordPress will create it on demand.

As a bonus, if you pass a width and height of the desired image to wp_get_attachment_image() (and related functions), you can create any size image. So for example I could create a thumbnail 50 pixels wide by 40 high of attachment 123 with the following code, where '50x40' is '[width]x[height]':


echo wp_get_attachment_image( 123, '50x40' );

Download

You can download the Custom Image Sizes plugin here.

41 Comments

  1. Hi. Great idea for a plugin! Had a quick feature request. Enabling the function to be able to create a cropped image would be ideal. I believe that extending:

    wp_get_attachment_image( 123, '50x40' );

    to:

    wp_get_attachment_image( 123, '50', '40', true );

    Would accomplish this rather well.

  2. That’s not a bad idea, Michael, but the problem is that wp_get_attachment_image() is a WordPress function; my plugin just filters some of the data it retrieves. So I can’t change the meaning or order of the function’s arguments.

    By default it crops the image, so maybe I could change it to accept just one of the dimensions to scale it, sorta like ImageMagick syntax:

    wp_get_attachment_image( 123, '50x' );

    I’ll have to think about that.

  3. Can you say more about configuring files to use your plugin?

    Say you have a magazine theme that uses 3 sizes as posts progress through 3 positions (based on age): 1)300×200 for position 1; 2) 260×80 for position 2; 3) 100×100 for position 3.

    So now — how you would configure your theme to crop correctly, and display in the page?

    Thank you

  4. great work! i’m calling attachment ID with this function:
    function attachment_ID(){
    // via mediathek
    $attachments = get_children( array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => 1, // show all -1
    'post_status' => 'inherit',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order ASC'
    ) );
    foreach ( $attachments as $attachment_id => $attachment ) {
    return $attachment_id;
    }
    }

  5. This is very handy, thx for sharing this, much appriciated!

  6. Hi Austin, I’m getting :

    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /wp-content/plugins/custom-image-sizes.php on line 13

    Any ideas what might be causing that?

  7. kwhitedesign,

    It sounds like you’re running PHP 4. My plugin requires that you be using at least PHP 5. (PHP 4 was deprecated in 2008 and no longer receives security patches. You should upgrade as soon as possible.)

    By the way, did you rename the file?

  8. Amazing plugin! I was racking my brain for a solution when I stumbled across this… problem solved.

  9. Great work – thanks!

  10. I needed to stop in and say that I just sent an email and stated that this plugin is my favorite WP plugin currently in existence. I’ve used it on so many projects. It has made many tasks SO easy for me. So…thank you so much for the wonderful plugin.

  11. This is so perfect! My magazine theme may have different sized thumbs according to section. And none of them shared the size of my featured slider. So when the article drops off of the featured slider and appears in it’s normal category… auto thumbnail magic occurs. I’ve been dying to break away from my Tim-thumb based cropper (hated the query string in the image url) and it was a hurdle I needed to cross when developing our next theme. You saved our bacon.
    Thanks so much!

  12. Hi filosofo!

    I activated the plug-in but nothing change.

    Do I have to add some code to the CSS or something like that?

    THANK YOU VERY MUCH in advance for any help!!!

    Best regard
    Fripp

  13. Hello,

    I installed your plugin but I am getting this error. I deactivated all the plugins and still was replicating the error.

    Thanks in advance!

    Warning: trim() expects parameter 1 to be string, array given in C:\wamp\www\wp-content\plugins\custom-image-sizes\filosofo-custom-image-sizes.php on line 31

  14. Thanks! At first it gave an error, but it seemed to clash somehow with my w3 cache plugin. After decaching everything, all seems fine for now.
    Best Regards!

  15. Dear Austin,

    I was wondering if wordpress would have an auto sizing plug in since we do have a yacht crew web site and the actual program is just focusing at the center of the photo but the face .
    would there be a resizing plug in where the end users can crop their own photos and publish the way they desire.
    thanks in advance
    cpt Oguz

  16. Glad to see that this site works well on my iPhone , everything I want to do is functional. Thanks for keeping it up to date with the latest.

  17. I was looking for something just like this. Having the ability to have custom sized images in WordPress is very handy.

  18. this wordpress plugin very useful for my blog thanx.

  19. Thank you for posting this great blog. The subject is intriguing to me.

  20. You’re stuck with existing thumbnails of the wrong size.

  21. Where did you got this much info on your blog from?? Also can i take the initiative to take the feeds from your blog for my own website??

  22. I have an attachment image of ID number 123, I might do something like the following:

  23. I make a lot of WordPress themes, and frequently clients want to associate a particular size of image with a post.

  24. I’m sorry for this intrusion, I see from your site that we are very much into the same things.

  25. So for example, if I have an attachment image of ID number 123, I might do something like the following:

  26. Hi. Nice plugin, thanks for it.

    And just a one question: how can i make scale square images with white background?

  27. It is important, therefore, to come up with a well-polished college entrance thesis writing help is offered at qualityresearchpaper.com article or graduate student university entrance article, such as MBA article.

  28. Statistics and business skills and non-traditional abilities necessary for getting the value of big information, as well as performers and developers for information creation. cheap swimwear

  29. Research and company abilities and non-traditional capabilities necessary for getting the value of big details, as well as artists and designers for details creation . free netflix account

  30. I think jesus was really just jealous that Indicate got to side sex-related attack the dude’s encounter before he did. http://www.coinvex.com/

  31. ‘Outta My Head’ is a appealing business for the new record, and before you get to the end, you will discover the term ‘can’t get you out of my head’ also is applicable to the music itself. look here

  32. Together with manufacturer Keith Stegall, Campbell designed a very 21st-century contemporary traditional(ish) nation audio for himself on his first appearance record, and it seems they, along with co-producer He Rovey, are not creating any needless manufacturing changes. portable vaporizer reviews 2014

  33. As far as the Mac executable is concerned, OSX will automatically prompt the user to download Java if it is not already installed. See this

  34. Noise could sometimes be a problem depending on where you are but it’s not as bad as in the very centre of town. It is a picturesque and cultural part of the city with a vibrant café culture and bar scene. accountants london

  35. And my addEvent also creates this make reference to item in query, even for IE. Juegos Friv

  36. That size doesn’t exist, WordPress just scales down the larger-sized original image, which might cause some performance issues. mlb predictions

  37. I’ve been dying to break away from my Tim-thumb based cropper (hated the query string in the image url) and it was a hurdle I needed to cross when developing our next theme. best folding bike

  38. Very nice plugin. And this site is very unique too.
    Virtual Assistant

  39. I could change it to accept just one of the dimensions to scale it, sorta like ImageMagick syntax. http://www.playfinity.co.uk/

  40. Very centre of town. It is a picturesque and cultural part of the city with a vibrant café culture and bar scene. http://www.love2drive.co

  41. I found this website that deals with private security and alarms and I thought it may be helpful to you and your readers. http://www.legalinv.com

Post a Comment

Your email is never shared. Required fields are marked *

*
*

14 Trackbacks