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.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | crop_resize_desc.patch | 794 bytes | sime |
| #2 | image_crop_0.patch | 2.02 KB | Steven |
| image_crop.patch | 2.02 KB | Steven |
Comments
Comment #1
ChrisKennedy commentedThis would be handy. Is that print_r() for debugging? And shouldn't it explicitly return FALSE if the resize fails?
Comment #2
Steven commentedI 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.
Comment #3
simeApplied patch, works as expected.
http://footy.emspace.com.au/node/19
Comment #4
simeForgot to mention. Patch applied to a D5 site, but I doubt that's a problem in this case.
Comment #5
dries commentedLooks good to me. Feel free to commit this Steven.
Maybe write TRUE instead of True?
Comment #6
drewish commentedI'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.
Comment #7
Steven commentedThe 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, whileimage_scale_and_crop()will return an image that is always equal to the target size.I'm not sure how to make it clearer...
Comment #8
dries commentedCurrent PHPdoc:
Maybe we can change it to:
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.
Comment #9
simeIt 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.
Comment #10
dries commentedI've integrated sime's suggestions, change the True to TRUE and committed the patch to CVS HEAD. Thanks all.
Comment #11
dwwupdate 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).
Comment #12
simeThanks dww. I've done this now.
http://drupal.org/node/114774#img-scale-crop
Comment #13
simeSorry Dries, I screwed up that description. Here's a quick patch hopefully.
Comment #14
dries commentedCommitted!
Comment #15
(not verified) commented