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