Make WordPress Core

Changeset 58612


Ignore:
Timestamp:
07/01/2024 08:43:55 PM (3 weeks ago)
Author:
SergeyBiryukov
Message:

REST API: Correct image cropping tools in the block editor.

As of [58457], the width and height cropping values are cast to an integer before the comparison to see if the target width and height differ from the original width and height.

Since they are now integers, it exposes a bug where the && of the if conditional meant that if you were only cropping in one dimension, the check wouldn't pass, and cropping would not occur.

In the block editor, the cropping tools are aspect ratio based, so one of the dimensions will always match that of the source image. Therefore, now that the values are cast as integers, the condition that allows a cropping to occur needs to be updated. If either width or height is different from the source image, then a crop should be allowed.

Follow-up to [50124], [58457].

Props andrewserong, jrf, kevin940726.
Fixes #61514. See #59782.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r58457 r58612  
    627627                    $height = (int) round( ( $size['height'] * $args['height'] ) / 100.0 );
    628628
    629                     if ( $size['width'] !== $width && $size['height'] !== $height ) {
     629                    if ( $size['width'] !== $width || $size['height'] !== $height ) {
    630630                        $result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
    631631
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r58457 r58612  
    23832383
    23842384    /**
     2385     * @ticket 61514
     2386     * @requires function imagejpeg
     2387     */
     2388    public function test_edit_image_crop_one_axis() {
     2389        wp_set_current_user( self::$superadmin_id );
     2390        $attachment = self::factory()->attachment->create_upload_object( self::$test_file );
     2391
     2392        $this->setup_mock_editor();
     2393        WP_Image_Editor_Mock::$size_return = array(
     2394            'width'  => 640,
     2395            'height' => 480,
     2396        );
     2397
     2398        WP_Image_Editor_Mock::$edit_return['crop'] = new WP_Error();
     2399
     2400        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2401        $request->set_body_params(
     2402            array(
     2403                'x'      => 50,
     2404                'y'      => 0,
     2405                'width'  => 10,
     2406                'height' => 100,
     2407                'src'    => wp_get_attachment_image_url( $attachment, 'full' ),
     2408
     2409            )
     2410        );
     2411        $response = rest_do_request( $request );
     2412        $this->assertErrorResponse( 'rest_image_crop_failed', $response, 500 );
     2413
     2414        $this->assertCount( 1, WP_Image_Editor_Mock::$spy['crop'] );
     2415        $this->assertSame(
     2416            array( 320, 0, 64, 480 ),
     2417            WP_Image_Editor_Mock::$spy['crop'][0]
     2418        );
     2419    }
     2420
     2421    /**
    23852422     * @ticket 44405
    23862423     * @requires function imagejpeg
Note: See TracChangeset for help on using the changeset viewer.