2

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

1 Answer 1

5

Custom Image Sizes

Resizing without cropping is already part of the core functionality, via add_image_size().

Note the last parameter:

<?php add_image_size( $name, $width, $height, $crop ); ?>

The Codex entry describes the $crop parameter as follows:

$crop

(boolean) (optional) Crop the image or not. False - Soft proportional crop mode ; True - Hard crop mode. Default: false

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:

add_image_size( 'image-size-name', $width, $height, false );

Note that this behavior is the default setting. In order to hard-crop, you have to explicitly specify $crop to be true.

Post Thumbnails

Changing the post-thumbnail image size to box-resize is similar; simply use set_post_thumbnail_size(), which also has a $crop parameter:

 <?php set_post_thumbnail_size( $width, $height, $crop ); ?>

Reserved Image Sizes

I'm pretty sure 'medium' and 'large' image sizes are already box-resized rather than hard-cropped.

Your Answer

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

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