Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#55247 new defect (bug)

SVG logos generate warnings from _wp_get_image_size_from_meta()

Reported by: gillespieza's profile gillespieza Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 5.9
Component: Media Keywords:
Focuses: Cc:

Description

If I set an SVG image as my logo in the customizer, my debug log gets filled with the following notices:

PHP Notice: Trying to access array offset on value of type bool in /{redacted}/wp-includes/media.php on line 1182

Looking at that file, that section refers to the following function:

<?php
function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
        if ( 'full' === $size_name ) {
                return array(
                        absint( $image_meta['width'] ),
                        absint( $image_meta['height'] ),
                );
        } elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
                return array(
                        absint( $image_meta['sizes'][ $size_name ]['width'] ),
                        absint( $image_meta['sizes'][ $size_name ]['height'] ),
                );
        }

        return false;
}

I suspect what's happening here is the function is assuming there will always be a height and width meta, which doesn't exist with SVG file types.

Change History (3)

#1 @nosilver4u
2 years ago

Since SVG uploads are not allowed by core currently, you're likely using a plugin, right? If so, it's up to the plugin to add a width/height, even if it's just arbitrary values. Otherwise, yes, you'll get all sorts of issues with core because the dimensions are used by core all over the place.
It's possible that can be changed if core ever adds support for SVG uploads, but sadly that's not a thing yet (probably a ticket for it somewhere though).

#2 @gillespieza
2 years ago

Ah, thanks. No, I'm not using a plugin for that, but a code snippet that filters upload_mimes and wp_check_filetype_and_ext. You wouldn't happen to know, off-hand, what filter I should use to set a height/width? I'm guessing wp_update_attachment_metadata?

#3 @nosilver4u
2 years ago

The two plugins I've looked at recently hook into wp_generate_attachment_metadata. Here's one example: https://plugins.trac.wordpress.org/browser/safe-svg/trunk/safe-svg.php#L371

Note: See TracTickets for help on using tickets.