[WIP] Fix reading of small SVG images to determine dimensions #1807
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This issue came up via this support topic: https://wordpress.org/support/topic/how-to-show-emoji-show-share-button-and-hide-powered-by-on-amp-page/
With this raw post content:
In HTML non-AMP this gets rendered as:
Note that the emoji character is being converted into an image by WordPress's emoji handling. In AMP this is disabled, so the character passes through as a character but the image is incorrectly rendered in AMP:
The underlying HTML is:
The problem turns out to be an issue with
FasterImage
and how it was modified in #1150 to allow extracting pixel dimensions from SVG images. The problem is that it is attempting to read 1024 bytes of data from the image, but the above SVG is only 525 bytes long. This results in aWillWashburn\Stream\StreamBufferTooSmallException
exception being thrown, and the image size failing to be obtained, and thus the fallback image size is being used (which is obviously too big).So this PR fixes that problem by allowing small SVG images to be read without throwing an error.
Work Outstanding
Before
After
While this is way better, it is still not perfect. The SVG image has a viewbox of 36x36 so that is what is assigned as the
width
&height
of theamp-img
. Nevertheless, the emojis are supposed to be 16x16. Or rather, they are supposed to be1em
by1em
, so it should look like this:So to full fix the case of inline SVG emojis we potentially need to either replace the
img.emoji
back with the underlying character (which is found in thealt
attribute). This would not be ideal because we'd miss out on the improvements that WordPress's emoji handling was introduced for.A full fix seems to be two-fold:
1em
somehow. This is a challenge becauseamp-img
doesn't allowem
units.amp-wp/includes/class-amp-theme-support.php
Lines 766 to 767 in c92e129
These may be out of scope for this PR.
Other outstanding work:
Fixes #204.