Looking for a hack to resize images on upload (all forms of image uploads on wordpress) to exact dimensions, but not to crop.
I will open a bounty for this question since I believe It can be of help for a lot of people.
The ideal solution must be as a plugin and using the image dimensions already specified on the Media Settings Page (options-media.php / Thumbnail , Medium and Large)
I know the ideal sometimes can’t be done, so the closest answer will be the accepted one
Custom Image Sizes
Resizing without cropping is already part of the core functionality, via
add_image_size()
.Note the last parameter:
The Codex entry describes the
$crop
parameter as follows:I prefer to describe the option as hard-crop vs. box-resize. Hard-crop will crop the image based on the most-restrained dimension, whereas box-resize will simply resize the image, retaining its original dimension proportions.
So, to create a box-resized image size, simply define it as follows:
Note that this behavior is the default setting. In order to hard-crop, you have to explicitly specify
$crop
to betrue
.Post Thumbnails
Changing the
post-thumbnail
image size to box-resize is similar; simply useset_post_thumbnail_size()
, which also has a$crop
parameter:Reserved Image Sizes
I’m pretty sure
'medium'
and'large'
image sizes are already box-resized rather than hard-cropped.