Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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