0

I am trying to define an image size of exactly 290 height x 180 width for all images uploaded to my WP site. I tried updating image sizes under Settings->Media->Image Sizes but these are only max values and some images end up coming out at 290 x 155 etc, is there a way to force WP to create an image at my desired size?

I am not using post thumbnails, I have built a plugin that uses wp_get_attachment_image() to retrieve images.

1 Answer 1

2

You'll want to use add_image_size() in your functions.php to tell Wordpress you'd like it to create a new image size when you upload images:

add_image_size( 'custom_image_size_name', 290, 180, true );

Using the value true will tell Wordpress you want it to crop the image to those exact dimensions.

Once you do that and upload an image, you can use 'custom_image_size_name' in wp_get_attachment_image() to get the correct image.

Because Wordpress only creates the image sizes when you upload images, you'll want to use a plugin like AJAX Thumbnail Rebuild to rebuild previously uploaded images with your newly defined images size.

4
  • And additionally, please remember that, whenever you are using new image sizes, WP is creating new physical file of that size. So creating new image sizes is a matter of disk space - just to keep that in mind for a large project. :) Jan 1, 2014 at 0:41
  • Just what I needed Stephen, thank you very much! @Mayeenul - thanks for the advice, i will defiantly keep that in mind, cheers! :)
    – RoseCoder
    Jan 1, 2014 at 1:13
  • Unless WP has modified their thumbnail stuff, you'll definitely want to rebuild your thumbnails with the plugin mentioned. You will literally pull your hair out wondering what is wrong if you don't.
    – Dan Gayle
    Jan 1, 2014 at 1:59
  • Fortunately the site is still under dev so no images intended to stay have been added yet! :)
    – RoseCoder
    Jan 1, 2014 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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