Fix incorrectly large reported height for animated gifs #182
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.
The problem
I'm working on a site which uses the
eleventy-image
webc component and ran into an issue where animated gifs were getting ridiculously highheight
attributes set on them which would break the page.It turns out this is because the base
height
value that sharp returns in its metadata for animated gifs is actually the sum of the height of every frame, meaning if you have a 720x720 animated gif with 100 frames in it, the reported height will come back as 72,000px. That doesn't seem helpful!The fix
Digging into sharp's docs, the metadata will include an additional
pageHeight
value for animated formats which actually reflects the dimensions that the image will be displayed at.I added a check to
Image.getFullStats
to swap in thispageHeight
value as the official height of the image when present, and that seems to have done the trick.