Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SVG throws an error #3

Open
hchouhan opened this issue Jan 15, 2022 · 1 comment
Open

Using SVG throws an error #3

hchouhan opened this issue Jan 15, 2022 · 1 comment

Comments

@hchouhan
Copy link
Member

WordPress/gutenberg#36603 (comment)

@hchouhan
Copy link
Member Author

hchouhan commented Jan 15, 2022

Solution by @strarsis

The SVG support plugins I have tried all don't filter the image dimensions of SVGs, at least incompletely
(see these posts/issues for plugin SVG Support; Safe SVG; WP SVG images).

A non-zero fallback would also suffice to fix this issue.

function _svg_dims_fallback_is_svg($image_attachment_id)
{
    $image_mime_type = get_post_mime_type($image_attachment_id);
    return 'image/svg+xml' === $image_mime_type;
}

function svg_dims_fallback($image, $attachment_id, $size, $icon)
{
    if (!_svg_dims_fallback_is_svg($attachment_id)) {
        return $image; // skip non-SVGs
    }

    // set some non-zero defaults to prevent division by zero in Gutenberg site-logo render
    if(!isset($image[1]) or $image[1] === 0) {
        $image[1] = 1;
    }
    if(!isset($image[2]) or $image[2] === 0) {
        $image[2] = 1;
    }

    return $image;
}
add_filter( 'wp_get_attachment_image_src', 'svg_dims_fallback', 10, 4 );

The filter has to be set up in a plugin so it runs before the Gutenberg site logo render function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant