Make WordPress Core

Opened 22 months ago

Closed 3 months ago

#62389 closed defect (bug) (invalid)

add_image_size "crop" does not crop

Reported by: kkmuffme Owned by:
Priority: normal Milestone: Future Release
Component: Media Version:
Severity: minor Keywords: has-patch close
Cc: Focuses: docs

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)

62389.diff (1.4 KB ) - added by geekofshire 22 months ago.
Attached a patch updating the comments of add_image_size()
img-800x600-true.jpg (53.2 KB ) - added by nickchomey 3 months ago.
crop = true
img-800x600-leftbottom.jpg (53.6 KB ) - added by nickchomey 3 months ago.
crop = left, bottom
img-800x600-righttop.jpg (52.9 KB ) - added by nickchomey 3 months ago.
crop = right, top
pexels-brett-sayles-5480779.jpg (1.1 MB ) - added by nickchomey 3 months ago.
original image

Download all attachments as: .zip

Change History (8)

#1 @desrosj
22 months ago

  • Focuses docs added
  • Keywords needs-patch added
  • Milestone Awaiting ReviewFuture Release

Thanks for this @kkmuffme. Would you be able to create a patch with a suggested alternative?

@geekofshire
22 months ago

Attached a patch updating the comments of add_image_size()

#2 @geekofshire
22 months ago

  • Keywords has-patch added; needs-patch removed

#3 @nickchomey
3 months ago

  • Keywords close added
  • Resolutioninvalid
  • Status newclosed

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

https://github.com/WordPress/wordpress-develop/blob/2001ef14e6bada1575d2e8b144efff4ce9c01dab/src/wp-includes/class-wp-image-editor-imagick.php#L361

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.

@nickchomey
3 months ago

crop = true

@nickchomey
3 months ago

crop = left, bottom

@nickchomey
3 months ago

crop = right, top

@nickchomey
3 months ago

original image

Note: See TracTickets for help on using tickets.