Image.inc has APIs with various useful features, but one thing it lacks is a function to scale an image to an exact size, rather than maintaining aspect ratio (like image_scale() does). This is a common behaviour for image uploads on some sites. The attached patch adds this.

This function is incredibly useful for stuff like avatars, but I'm NOT going to deal with using this anywhere in core, since this would be a can of worms. This patch is only about filling a hole in image.inc.

Comments

ChrisKennedy’s picture

This would be handy. Is that print_r() for debugging? And shouldn't it explicitly return FALSE if the resize fails?

Steven’s picture

StatusFileSize
new2.02 KB

I thought we usually avoided statements like "return FALSE", since null is falsy enough for 99% of all use cases. The Image API seems to apply this consistently though.

Rerolled without debug code.

sime’s picture

Applied patch, works as expected.
http://footy.emspace.com.au/node/19

sime’s picture

Forgot to mention. Patch applied to a D5 site, but I doubt that's a problem in this case.

dries’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me. Feel free to commit this Steven.

True or FALSE, based on success

Maybe write TRUE instead of True?

drewish’s picture

I'd like a bit better documentation does, just glancing at the code and existing comments it's not clear what this would do to an image. I'm assuming that it reduces the image so the smaller side fits within the dimensions and then crops for the aspect ratio? Either way it would be nice to have it spelled out.

Steven’s picture

The function is called "scale and crop". The added doxygen makes it clear that image_scale() will return an image that is smaller than or equal to the target size, and will respect aspect ratio, while image_scale_and_crop() will return an image that is always equal to the target size.

I'm not sure how to make it clearer...

dries’s picture

Current PHPdoc:

Scales an image to the given width and height by scaling and cropping.
The resulting image always has the exact target dimensions.

Maybe we can change it to:

Scales an image to the given width and height by maintaining the original's file aspect ratio, then crops the image to the specified width and height.  This function is, for example, useful to create uniform avatars from larger photos.
The resulting image always has the exact target dimensions.

drewish: how does that sound?

A remaining question might be: how does it crop? Does it crop from the top/left, or from the right/bottom, or does it crop along all edges?

Anyway, it's a small little function that combines two other API functions in a convenient way.

sime’s picture

It crops the image equally on the each side (be it left/right or top/bottom. I've modified the description to reflect this, picked up some typos too.

Scales an image to the exact width and height given. Maintains the original file's aspect ratio by cropping the image equally on both sides, or equally on the top and bottom.  This function is, for example, useful to create uniform avatars from larger photos.
dries’s picture

Status: Reviewed & tested by the community » Fixed

I've integrated sime's suggestions, change the True to TRUE and committed the patch to CVS HEAD. Thanks all.

dww’s picture

Status: Fixed » Needs work

update docs? http://drupal.org/node/114774/edit
(just being diligent when i see cvs updates into my HEAD workspace that are changes to the API which aren't documented in the upgrade docs).

sime’s picture

Status: Needs work » Fixed

Thanks dww. I've done this now.
http://drupal.org/node/114774#img-scale-crop

sime’s picture

Status: Fixed » Needs review
StatusFileSize
new794 bytes

Sorry Dries, I screwed up that description. Here's a quick patch hopefully.

dries’s picture

Status: Needs review » Fixed

Committed!

Anonymous’s picture

Status: Fixed » Closed (fixed)