News: Illegal string offset warning for Height and Width
-
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.
- The topic ‘News: Illegal string offset warning for Height and Width’ is closed to new replies.