Make WordPress Core

Ticket #64056: svg-rest-api-fix.patch

File svg-rest-api-fix.patch, 2.1 KB (added by sachinrajcp123, 5 months ago)
  • wp-admin/includes/image.php

    diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php
    index 6e2b5f1..f17a1f2 100644
    a b function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    106106       $metadata = array();
    107107
    108108       if ( preg_match( '!^image/!', get_post_mime_type( $attachment_id ) ) ) {
    109                $imagesize = getimagesize( $file );
    110                $metadata['width']  = $imagesize[0];
    111                $metadata['height'] = $imagesize[1];
     109               // Skip dimension checks for SVG since they are vector-based.
     110               $type = wp_check_filetype( $file );
     111               if ( isset( $type['ext'] ) && $type['ext'] === 'svg' ) {
     112                       $metadata['width']  = 0;
     113                       $metadata['height'] = 0;
     114               } else {
     115                       $imagesize = @getimagesize( $file );
     116                       if ( $imagesize ) {
     117                               $metadata['width']  = $imagesize[0];
     118                               $metadata['height'] = $imagesize[1];
     119                       }
     120               }
    112121       }
    113122
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

            return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
    diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index 5d3f5f4..7f31a8c 100644
    a b class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    691691               $media_details['sizes'] = array();
    692692       }
    693693
     694       // Ensure SVGs don’t include invalid width/height in REST responses.
     695       if ( isset( $response->data['mime_type'] ) && $response->data['mime_type'] === 'image/svg+xml' ) {
     696               unset( $media_details['sizes'] );
     697               unset( $media_details['height'] );
     698               unset( $media_details['width'] );
     699       }
     700
    694701       $response->data['media_details'] = $media_details;
    695702
    696703       return $response;