Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#64056 new enhancement

Media / REST API: Uploading non-resizable image formats (SVG) generates multiple PHP warnings

Reported by: jigneshnakrani's profile jignesh.nakrani Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch needs-testing close
Focuses: rest-api Cc:

Description

When uploading an SVG file using the WordPress Media REST API (e.g., via /wp/v2/media), the response includes several PHP warnings. These warnings indicate undefined array keys for "width" and "height" in image.php, followed by multiple "Cannot modify header information" errors in class-wp-rest-server.php. The SVG file uploads successfully, but the warnings clutter the API response, which can disrupt client-side applications expecting clean JSON output.

Steps to Reproduce:

  • Set up a WordPress installation with SVG support enabled (e.g., using a plugin like Safe SVG or custom code).
    // Allow SVG file to be uploaded.
    add_filter( 'upload_mimes', 'custom_mime_types' );
        
    function custom_mime_types( $mimes ) {
        $mimes['svg'] = 'image/svg+xml';
    
        return $mimes;
    }
    
  • Use the REST API endpoint /wp/v2/media to upload an SVG file (e.g., via a POST request with a valid SVG file and appropriate authentication).
  • Observe the API response, which includes the following warnings:
<br />
<b>Warning</b>:  Undefined array key "width" in <b>/var/www/src/wp-admin/includes/image.php</b> on line <b>110</b><br />
<br />
<b>Warning</b>:  Undefined array key "height" in <b>/var/www/src/wp-admin/includes/image.php</b> on line <b>111</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /var/www/src/wp-admin/includes/image.php: 110) in <b>/var/www/src/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1896</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /var/www/src/wp-admin/includes/image.php: 110) in <b>/var/www/src/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1896</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /var/www/src/wp-admin/includes/image.php: 110) in <b>/var/www/src/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1896</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /var/www/src/wp-admin/includes/image.php: 110) in <b>/var/www/src/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1896</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /var/www/src/wp-admin/includes/image.php: 110) in <b>/var/www/src/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1920</b><br />

Expected Behavior:

The SVG file should upload successfully via the REST API, and the response should be a clean JSON object containing the media details without any PHP warnings.

Additional Context:

  • The issue appears to stem from wp-admin/includes/image.php attempting to access width and height keys in the image metadata, which are not applicable to SVG files (as SVGs are vector-based and do not have fixed pixel dimensions).
  • The subsequent header warnings in class-wp-rest-server.php are likely caused by the earlier warnings outputting text before headers are sent, breaking the REST API's ability to set headers properly.
  • Tested on a fresh WordPress install with minimal plugins to confirm the issue persists.

Attachments (1)

svg-rest-api-fix.patch (2.1 KB) - added by sachinrajcp123 6 weeks ago.

Download all attachments as: .zip

Change History (5)

This ticket was mentioned in PR #10095 on WordPress/wordpress-develop by @jignesh.nakrani.


6 weeks ago
#1

  • Keywords has-patch added

#2 @jignesh.nakrani
6 weeks ago

  • Keywords needs-testing added

#3 @jorbin
6 weeks ago

  • Component changed from REST API to Media
  • Keywords close added

As SVG uploads are not supported out of the box by core, this feels like something that the plugin which adds the support should be fixing. Moving to media for visibility, but I think this should be closed as wontfix

Note: See TracTickets for help on using tickets.