Skip to content

Commit 4262702

Browse files
committed
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]. Reviewed by desrosj. Backports [60195] to the 6.8 branch. Props sirlouen, adamsilverstein, audrasjb, pbiron, sainathpoojary, dilipbheda, pratiklondhe. Fixes #63302. See #61167. git-svn-id: https://develop.svn.wordpress.org/branches/6.8@60196 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b931715 commit 4262702

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,16 @@ public function create_item_permissions_check( $request ) {
155155
isset( $files['file']['type'] ) &&
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',
162170
__( 'The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.' ),

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
3232
*/
3333
private static $test_avif_file;
3434

35+
/**
36+
* @var string The path to the SVG test image.
37+
*/
38+
private static $test_svg_file;
39+
3540
/**
3641
* @var array The recorded posts query clauses.
3742
*/
@@ -115,6 +120,12 @@ public function set_up() {
115120
copy( $orig_avif_file, self::$test_avif_file );
116121
}
117122

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 );
127+
}
128+
118129
add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
119130
add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
120131
}
@@ -2603,4 +2614,30 @@ public function test_upload_unsupported_image_type_with_filter() {
26032614

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
}

0 commit comments

Comments
 (0)