I am trying to generate thumbnail according to image width or height. set image width 200px if width is larger then height or set image height 200px if height is larger then width.

  • 1
    What do you have in your media settings screen? What have you tried so far? What do you currently get? Where are you displaying your thumbnails? What is the code that displays them? – Tom J Nowell Dec 11 '14 at 21:04
  • Currently I'm using basic code add_image_size('artwork-size', 200, 9999, false); this is what i want if width and height equals to each other add_image_size('artwork-square', 200, 200, false); elseif width greater then height then add_image_size('artwork-landscape', 200, 9999, false); elseif height greater then width then add_image_size('artwork-portrait', 9999, 200, false); – Prem Vishrant Dec 11 '14 at 22:10
  • add_image_size should always be called, think of it more as register_image_size – Tom J Nowell Dec 11 '14 at 23:36

add_image_size doesn't do what you think it does. It doesn't add an image of size X to a thumbnail, rather it registers an image size, and its associated dimensions. register_image_size may well have been the more appropriate name for this function.

As a result, you need to register all your image sizes, on every page load. If you don't, when you upload an image, it won't be resized to that image size.

To use an image size, pass in its name when calling other functions, e.g.:

the_post_thumbnail( 'thumbnail' );
the_post_thumbnail( 'medium' );
the_post_thumbnail( 'large' );

// and so on...

the_post_thumbnail( 'artwork-square' );
the_post_thumbnail( 'artwork-landscape' );

A final note. If you haven't called add_image_size to register your image size, and you upload an image, a copy of that image in the new size will not be made. If you then call and use add_image_size again, or change the dimensions, WordPress will not go back and resize/recreate all those images ( imagine it, your server would become unresponsive resizing hundreds or thousands of images ).

Instead WordPress will use the closest available size, and scale it up in html, which can cause stretching and quality issues as the browser widens and lengthens a small image.

To get around this, there are plugins that regenerate the thumbnails. There are several available on wordpress.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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