• Hi!
    The original Thread is 11 months old and closed, but since i got a working solution (hopefully, I found no errors, but please test some more), I started a new one on this.

    The problem is missing image-metadata that should contain an array of all imagesizes and their filenames, which is generated at upload-time by wp_generate_attachment_metadata(). Some functions in wp – like the renderer used by image-widget – read that info and throw the warning, when its not found.

    wp_generate_attachment_metadata() got a filter-hook, so I added this function to attachment-modal.php:

    function generate_svg_attachment_metadata( $metadata, $attachment_id ) {
        
        $mime = get_post_mime_type( $attachment_id );
        if ( $mime == 'image/svg+xml' ) {
            $svg_path = get_attached_file( $attachment_id );
            $upload_dir = wp_upload_dir();
            // get the path relative to /uploads/ - found no better way:
            $relative_path = str_replace($upload_dir['basedir'], '', $svg_path);
            $filename = basename( $svg_path );
            
            $dimensions = bodhi_svgs_get_dimensions( $svg_path );
    
            $metadata = array(
                'width' => intval($dimensions->width),
                'height' => intval($dimensions->height),
                'file' => $relative_path
            );
    
            // Might come handy to create the sizes array too - But it's not needed for this workaround! Always links to original svg-file => Hey, it's a vector graphic! ;)
            $sizes = array();
            foreach ( get_intermediate_image_sizes() as $s ) {
                $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
                if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
                    $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
                else
                    $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
                if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
                    $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
                else
                    $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
                if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
                    $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
                else
                    $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
                
                $sizes[$s]['file'] =  $filename;
                $sizes[$s]['mime-type'] =  'image/svg+xml';
            }
            $metadata['sizes'] = $sizes;
        }
    
        return $metadata;
    }
    add_filter( 'wp_generate_attachment_metadata', 'generate_svg_attachment_metadata', 10, 3 );

    ATTENTION: This only works for new image upload!!
    I did not add code to update meta for existing images, because there are already plenty plugins doing this, like “Regenerate Thumbnails” or CLI (“wp media regenerate”).
    I am not sure if opening existing files in Media Manager and changing any field should also do the trick.

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

    (@benbodhi)

    Hey, thank you for reaching out and providing this info!

    As soon as I can, I will test and see if I can roll in your solution ??

    Thread Starter surml

    (@surml)

    You’re very welcome ?? glad when I can help!

    nicholascoughlin

    (@nicholascoughlin)

    Ben,

    Thank you so much for this amazing plugin.
    I also am experiencing this warning issue. Any chance this snippet from surml will get rolled in soon?

    Or short of that, is attachment-modal.php a file in the plugin? I can add this function to the plugin files but then it will just get overwritten the next time the plugin gets updated…

    Thank you,

    Nick

    Thread Starter surml

    (@surml)

    Jumping in just for the quick-fix:
    Yes, attachment-modal.php is a plugin’s file (inside the “functions”-folder) and yes, it will be overwritten in case of an update.

    @benbodhi could you possibly release a minor upgrade with a fixed version? I had to manually edit multiple client installations already and this would really help to stay on the safe side.

    Regards
    Surml

    Plugin Author Benbodhi

    (@benbodhi)

    Hey guys,
    I’m sorry, life has been crazy lately.

    I will try to get this included this week.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘News: Illegal string offset warning for Height and Width’ is closed to new replies.