Opened 19 months ago
Closed 5 days ago
#62389 closed defect (bug) (invalid)
add_image_size "crop" does not crop
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Future Release | Priority: | normal |
| Severity: | minor | Version: | |
| Component: | Media | Keywords: | has-patch close |
| Focuses: | docs | Cc: |
Description
e.g. take an 500x500 picture and run this code:
<?php $editor = wp_get_image_editor( $path_to_a_jpg ); if ( $editor instanceof WP_Image_Editor_Imagick ) { $editor->make_subsize( array( 'width' => 147, 'height' => 147, 'crop' => true, ) ); }
Whether you run it with crop true or false, you get the same image.
Bc logically, "crop" in WP context only refers to the initial dimensions, not the final dimensions.
If you look at https://developer.wordpress.org/reference/functions/add_image_size/
Image cropping behavior. If false, the image will be scaled (default).
If true, image will be cropped to the specified dimensions using center positions.
However, this is misleading. Since the image will be scaled in BOTH cases.
The difference is not that the image gets scaled in one, but not scaled in the other.
The difference is simply, that crop true guarantees that both specified dimensions are used.
While crop false, only ensures this for one dimension.
I guess this should be improved in the docs?
Attachments (5)
Change History (8)
#1
@
19 months ago
- Focuses docs added
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
#3
@
5 days ago
- Keywords close added
- Resolution set to invalid
- Status changed from new to closed
It seems to me that the original #62388 ticket was actually correct and this one is wrong. Crop is not *just* a boolean. It can be an array saying where to crop from.
e.g. add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); Hard crop left top
x_crop_position accepts ‘left’ ‘center’, or ‘right’.
y_crop_position accepts ‘top’, ‘center’, or ‘bottom’.
https://developer.wordpress.org/reference/functions/add_image_size/#crop-mode
And then when the image editor resize function is called, it first calls image_resize_dimensions
image_resize_dimensions then uses the crop value - either a boolean defaults to center, center or an array uses the provided crop positions and ultimately generates the coordinates for cropping.
Then resize() calls crop(), which crops. Then it does the resize via thumbnail_image.
So, if two subsizes have the same output dimensions but different crops, then the 2nd will overwrite the first despite being different.
I just confirmed this with this MU plugin and step debugging to pause after generating each image and then move/rename it.
<?php
function duplicate_subsizes_issue() {
// Register multiple image sizes with the same dimensions and crop settings
add_image_size( 'custom-size-1', 800, 600, true);
add_image_size( 'custom-size-2', 800, 600, array( 'left', 'bottom' ) );
add_image_size( 'custom-size-3', 800, 600, array( 'right', 'top' ) );
}
add_action( 'after_setup_theme', 'replicate_subsizes_issue' );
See the following 3 attachments - there's a subtle left/right shift in each.
So, I'm closing this and re-opening #62388.
Thanks for this @kkmuffme. Would you be able to create a patch with a suggested alternative?