Make WordPress Core

Changeset 60195


Ignore:
Timestamp:
04/28/2025 03:37:27 PM (7 weeks ago)
Author:
jorbin
Message:

Media: Don't try to resize image formats which can't be resized

While having a mime type with an "image" prefix, SVG images are in fact "Scalable Vector Graphics" that can be scaled directly.

Follow-up to [60084].

Props sirlouen, adamsilverstein, audrasjb, pbiron, sainathpoojary, dilipbheda, pratiklondhe.
Fixes #63302. See #61167.

Location:
trunk
Files:
2 edited

Legend:

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

    r60084 r60195  
    156156            str_starts_with( $files['file']['type'], 'image/' )
    157157        ) {
    158             // Check if the image editor supports the type.
    159             if ( ! wp_image_editor_supports( array( 'mime_type' => $files['file']['type'] ) ) ) {
     158            // List of non-resizable image formats.
     159            $editor_non_resizable_formats = array(
     160                'image/svg+xml',
     161            );
     162
     163            // Check if the image editor supports the type or ignore if it isn't a format resizable by an editor.
     164            if (
     165                ! in_array( $files['file']['type'], $editor_non_resizable_formats, true ) &&
     166                ! wp_image_editor_supports( array( 'mime_type' => $files['file']['type'] ) )
     167            ) {
    160168                return new WP_Error(
    161169                    'rest_upload_image_type_not_supported',
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r60084 r60195  
    3434
    3535    /**
     36     * @var string The path to the SVG test image.
     37     */
     38    private static $test_svg_file;
     39
     40    /**
    3641     * @var array The recorded posts query clauses.
    3742     */
     
    114119        if ( ! file_exists( self::$test_avif_file ) ) {
    115120            copy( $orig_avif_file, self::$test_avif_file );
     121        }
     122
     123        $test_svg_file       = DIR_TESTDATA . '/uploads/video-play.svg';
     124        self::$test_svg_file = get_temp_dir() . 'video-play.svg';
     125        if ( ! file_exists( self::$test_svg_file ) ) {
     126            copy( $test_svg_file, self::$test_svg_file );
    116127        }
    117128
     
    26042615        $this->assertSame( 201, $response->get_status() );
    26052616    }
     2617
     2618    /**
     2619     * Test that uploading an SVG image doesn't throw a `rest_upload_image_type_not_supported` error.
     2620     *
     2621     * @ticket 63302
     2622     */
     2623    public function test_upload_svg_image() {
     2624        wp_set_current_user( self::$editor_id );
     2625        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2626        $request->set_header( 'Content-Type', 'image/svg+xml' );
     2627        $request->set_file_params(
     2628            array(
     2629                'file' => array(
     2630                    'file'     => file_get_contents( self::$test_svg_file ),
     2631                    'name'     => 'video-play.svg',
     2632                    'size'     => filesize( self::$test_svg_file ),
     2633                    'tmp_name' => self::$test_svg_file,
     2634                    'type'     => 'image/svg+xml',
     2635                ),
     2636            )
     2637        );
     2638        $rest_controller = new WP_REST_Attachments_Controller( 'attachment' );
     2639        $result          = $rest_controller->create_item_permissions_check( $request );
     2640
     2641        $this->assertTrue( $result );
     2642    }
    26062643}
Note: See TracChangeset for help on using the changeset viewer.