wp-rocket
wp-rocket copied to clipboard
Add Missing Image Dimensions throws PHP Warning: simplexml_load_file() [...] when relative path is used for SVG
Before submitting an issue please check that you’ve completed the following steps:
- Made sure you’re on the latest version - 3.11.2
- Used the search feature to ensure that the bug hasn’t been reported before - yes
Describe the bug The following PHP warning is being logged when a relative path is used for SVG.
PHP Warning: simplexml_load_file(): %2Fhome4%2Ftest%2Ftestsite.com%2Fwp-content%2Fgoogle-play-badge.svg:30: namespace warning : xmlns: URI &ns_sfw; is not absolute in /home4/test/testsite.com/wp-content/plugins/wp-rocket/inc/Engine/Media/ImageDimensions/ImageDimensions.php on line 496
PHP Warning: simplexml_load_file(): <sfw xmlns="&ns_sfw;"> in /home4/test/testsite.com/wp-content/plugins/wp-rocket/inc/Engine/Media/ImageDimensions/ImageDimensions.php on line 496
PHP Warning: simplexml_load_file(): ^ in /home4/test/testsite.com/wp-content/plugins/wp-rocket/inc/Engine/Media/ImageDimensions/ImageDimensions.php on line 496
To Reproduce Steps to reproduce the behavior:
- Add an SVG
your-svg.svg
to the server (in my case in/wp-content/
directory). - Add the following markup on the post with HTML block:
<img alt="Google Play" src="/wp-content/your-svg.svg" class="google-play">
- Enable
Add Missing Image Dimensions
option in WP Rocket settings. - Preload or visit the page.
- Debug log will have the PHP warning logged
Expected behavior no warnings should be logged
Additional context Ticket: https://secure.helpscout.net/conversation/1899887147/345420?folderId=3864740
Backlog Grooming (for WP Media dev team use only)
- [ ] Reproduce the problem
- [ ] Identify the root cause
- [ ] Scope a solution
- [ ] Estimate the effort
Reproduce the problem
I was able to reproduce the error with a diffrent svg using the one from this issue: https://forge.typo3.org/issues/86428
Identify the root cause
The root cause I that the content from the SVG is malformed and that generate a warning.
Scope a solution
To solve this problem we can simply add a new param to not show warnings from the function.
$svgfile = simplexml_load_file( rawurlencode( $filename ), 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING );
This is the only problem as dimensions are loaded on the final SVG.
Estimate the effort
[xs]