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.

67 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

  42. That size doesn’t exist, WordPress just scales down the larger-sized original image, which might cause some performance issues. comment pirater facebook

  43. thanks for the info,ireally like this plugin i will try it in my game flash site:friv and ttt4

  44. Quatre individus ont été interpellés ce Mercredi 20 Novembre 2013 par une équipe de. iphone porn

  45. very good article and very rewarding good work! Thanks
    kizi

  46. 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. letmewatchthis

  47. When you buy exclusive paintings you are making a great financial commitment – an financial commitment in art, and an financial commitment in yourself. reference link

  48. Desplazandolo hacia el pelo cuantas mas angry birds que golpee, puedes conseguir puntos en el entretenimiento Con El Fin De avanzar en mas emocionantes etapas posteriores. SEO

  49. You activate it, and if you allp get attachment image and related functions for an attachment that doesn’t have that size, WordPress will create it on demand. stream Guardians of the Galaxy

  50. While I don’t use WordPress, I know a number of other bloggers that do, so this is great to pass along to them. Thanks! candy crush

  51. I think jesus was really just jealous that Indicate got to side sex-related attack. buy vine followers

  52. Country developed to sound for a Surprise Warning, we can do it for you. Last Weekend, It appears to be like you are operating PHP 4. My plug-in needs that you be. reference link

  53. I found this website that deals with home improvement in hobe sound fl and I thought it may be helpful to you and your readers.http://www.precisehomerenovations.com

  54. I found this website that deals with home improvement in hobe sound fl and I thought it may be helpful to you and your readers. http://www.carmelhomesllc.com/

  55. Do you need a better price on Car insurance in North Ridgeville Ohio? If you want to shop your auto insurance rates with a real, professional agency, contact us today. http://www.nevermaninsurance.com

  56. Changes to community costs, especially for knowledge, cannot be done in a straight line way or without concern. They should always consist of wide-ranging conversation among stakeholders on what nations want to accomplish and the appropriate way to accomplish. tarot reading

  57. Au plus mal ? Tu as entendu l’histoire de dingue ? DSK se fait arrêter à JFK, donc l’autre JFK, le révolté bayroutiste, s5620 au créneau et trousse une domestique à la stereo puis, se rendant compte de sa bévue, stability add sa défense que les. additional info read

  58. Can you say more about configuring files to use your plugin? dental deep cleaning

  59. 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: mortgage fraud attorney

  60. Ta carrière dans la politique, sincèrement, je n’y crois pas des public. Non que tu possèdes. assurance expatrié

  61. By standard it plants the picture, so maybe I could modify it to agree to just one of the. shemale webcam

  62. WordPress just scales down the larger-sized original image, which might cause some performance issues. seoveinte

  63. By standard it plants the picture, so maybe I could modify it to agree to just one of the. James

  64. It is then our intention to start the transition at two per month. Logistically it takes time to swap out a technician to a new van and we do not want to overwhelm ourselves, although the sooner we switch the sooner we start saving. overview

  65. Making business in Manhattan is not easy, but with a right approach it can generate higher-than-average incomes. And that to a great extent depends on the location of the business. Since every business is a unique one its requirements to retail space are unique as well. Use a help of New York City retail space leasing and sales experts at no charge. http://www.retail-officespace.com/

  66. If you need help getting auto insurance in houston but are not sure how to get the best price. Read this article and you xcould find additional saving methods to help save you money. http://www.rtinkerinsurance.com

  67. Powered by the financial doubt of previous times several decades, the U.S. devices lease industry is. manifestation miracle destiny

Post a Comment

Your email is never shared. Required fields are marked *

*
*

14 Trackbacks