Steve Taylor photo

Setting the image crop position in WordPress

Here’s a nice addition to WordPress, which I missed in last year’s 3.9 release. Now, when you define an image size with add_image_size, instead of just saying ‘soft crop’ (keep proportions) or ‘hard crop’ (fit to area), you can now also pass an array to define where you want a hard crop to attack the image from. For example:

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) );

The first element in the ‘crop array’ can be ‘left’, ‘center’, or ‘right’; the other can be ‘top’, ‘center’, or ‘bottom’. Nice!

As an image size bonus, here’s what I came up with recently when I wanted all WordPress image sizes to be controlled via custom theme code (not just the custom sizes).

Leave a comment

Your email address will not be published. Required fields are marked *