• Resolved strarsis

    (@strarsis)


    When using wp_get_attachment_image(…) to properly output the HTML for an image (including srcset/sizes, etc), for SVG images the width + height is 1px.

    Related WordPress core issue: https://core.trac.ww.wp.xz.cn/ticket/26256

    This plugin seems to be a great place for applying the patch mentioned in the issue ticket.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Benbodhi

    (@benbodhi)

    Hi @strarsis,

    Thanks for your suggestion.

    I played with that a few years ago… But will take another look when I get a chance. I have been thinking about different options when it comes to how image attributes are handled.

    Plugin Author Benbodhi

    (@benbodhi)

    Hi there,

    I finally got a chance to spend some time on the plugin…
    I believe this ticket refers to the admin not displaying the SVG thumbnails due to sizing, which is already implemented in my plugin.

    Correct me if I’m wrong though.

    Thread Starter strarsis

    (@strarsis)

    @benbodhi: Feedback after some time:
    I just encountered the issue again.
    It is not just the preview in admin/backend –
    all images with markup from wp_get_attachment_image(...) are affected,
    so also in frontend SVG images collapse to 1px x 1px dimensions.
    There is a fix in WordPress for this –
    this plugin would be ideal for this (to make WordPress supporting SVGs).

    Edit: When the issue has been fixed in WordPress core, the plugin could add a check for WordPress version and only apply the fix if the WordPress version is still affected.

    Thanks in advance!

    • This reply was modified 8 years, 6 months ago by strarsis.
    Plugin Author Benbodhi

    (@benbodhi)

    I’m not sure I see your issue.
    Are you using the latest version of my plugin and WordPress?

    Thread Starter strarsis

    (@strarsis)

    @benbodhi: Yes, I am using latest WordPress, WooCommerce and your plugin.
    I assigned a SVG image to a WooCommerce category.
    While the raster PNG images are shown correctly, the SVGs aren’t visible and only got a dimension of 1px x 1px.

    Plugin Author Benbodhi

    (@benbodhi)

    I see, I’ll try and work in some support for that in the next update.
    In the meantime, you can likely just use css to fill the frame.

    Thread Starter strarsis

    (@strarsis)

    @benbodhi:

    I am using the code from the discussion on the WordPress issue tracker:

    
    // Code from:
    // https://wordpress.stackexchange.com/questions/240579/issue-with-wp-get-attachment-image-and-svg-file-type
    // https://core.trac.ww.wp.xz.cn/ticket/26256
    
    /**
     * Removes the width and height attributes of <img /> tags for SVG
     * 
     * Without this filter, the width and height are set to "1" since
     * WordPress core can't seem to figure out an SVG file's dimensions.
     * 
     * For SVG:s, returns an array with file url, width and height set 
     * to null, and false for 'is_intermediate'.
     * 
     * @wp-hook image_downsize
     * @param mixed $out Value to be filtered
     * @param int $id Attachment ID for image.
     * @return bool|array False if not in admin or not SVG. Array otherwise.
     */
    function wpse240579_fix_svg_size_attributes( $out, $id ) {
        $image_url  = wp_get_attachment_url( $id );
        $file_ext   = pathinfo( $image_url, PATHINFO_EXTENSION );
    
        if ( is_admin() || 'svg' !== $file_ext ) {
            return false;
        }
    
        return array( $image_url, null, null, false );
    }
    add_filter( 'image_downsize', 'wpse240579_fix_svg_size_attributes', 10, 2 ); 
    
    • This reply was modified 8 years, 6 months ago by strarsis.
    Plugin Author Benbodhi

    (@benbodhi)

    Thanks for the update.
    I will test when I’m working on the plugin next.
    I thought it worked already for WooCommerce categories and this fix looks very similar to what I had to implement to avoid this issue in the media library thumbnails.

    Thanks for your support.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘img dimensions’ is closed to new replies.