Crop image without scaling or resizing with add_image_size()

Both of these tend to shrink the image and then crop it to the dimensions listed.

What I want is to just crop out the center of the image at 475 x 310

add_image_size( 'test', 475, 310, array( 'center', 'center' ) );
add_image_size( 'test2', 475, 310, true );

BTW I’m using the regenerate-thumbnails plugin to recreate all the images after I make a new add_image_size() deceleration.

Here is an image 1400 x 788 pixels, where I added a pink box directly in the center which is 475 x 310 pixels
enter image description here

I’ve uploaded the image to though my post. Rather than what I want which is crop the image at the pink box it is first scaled down and then cropped to the smaller dimension. Seen below is the new image from my wp-content/uploads directory produced from add_image_size( 'test', 475, 310, array( 'center', 'center' ) ); also the same result when I change that bit to add_image_size( 'test', 475, 310, TRUE );
enter image description here

What am I doing wrong?

1 Answer
1

You cant do that with the add_image_size() function, after a lot of research and digging in the core files, what the Hard Crop option of the add_image_size() function actually does is resize the image using the lower dimension, then it will crop the size specified

by example if you create a new image size:

add_image_size( 'test2', 475, 310, true );

and upload an image like this one:

enter image description here

it will resize the image using the height 310 (since is the lower value) keeping the aspect ratio, like this:

enter image description here

then it will proceed to crop the image using the resized image, based in the position that was send or the default center , center, the red square overlapping is the size 475, 310 and is the area that will be cropped:

enter image description here

enter image description here

enter image description here

it may look like the top and bottom dont matter, but that is because the image was resized using the height, if it was the other way around, the width being the lower the image would be tall and the left and right would look like they dont matter.

To accomplish what you are trying to do, you will need a plugin or a developer who will add the necessary code to your theme, it cant be done right now with the default built-in tools of wordpress.

These functions are the ones that are doing all this:

  • multiresize
  • _resize
  • image_resize_dimensions

Leave a Comment