Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrectly large reported height for animated gifs #182

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

Gyanreyer
Copy link
Contributor

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 high height 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 this pageHeight value as the official height of the image when present, and that seems to have done the trick.

@@ -415,6 +415,15 @@ class Image {
metadata.height = width;
}

if(metadata.pageHeight) {
// When the { animated: true } option is provided to sharp, animated
// image formats like gifs or webp will have an inaccurate `height` value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-page/frame images are processed by libvips as a "toilet roll" of images, which is why the height is the total of all frames, and therefore the page height is also made available. Hope this helps explain the situation.

@monochromer
Copy link

Is something blocking the merge of this PR?

@zachleat
Copy link
Member

Shipping with v3.1.9, thank you!

@zachleat
Copy link
Member

zachleat commented Feb 2, 2024

Changed to v3.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants