Make WordPress Core

Opened 3 weeks ago

Last modified 7 days ago

#62389 new defect (bug)

add_image_size "crop" does not crop

Reported by: kkmuffme's profile kkmuffme Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version:
Component: Media Keywords: has-patch
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 (1)

62389.diff (1.4 KB) - added by geekofshire 7 days ago.
Attached a patch updating the comments of add_image_size()

Download all attachments as: .zip

Change History (3)

#1 @desrosj
3 weeks ago

  • Focuses docs added
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

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

@geekofshire
7 days ago

Attached a patch updating the comments of add_image_size()

#2 @geekofshire
7 days ago

  • Keywords has-patch added; needs-patch removed
Note: See TracTickets for help on using tickets.