Skip to content

Commit

Permalink
Merge pull request #182 from Gyanreyer/main
Browse files Browse the repository at this point in the history
Fix incorrectly large reported height for animated gifs
  • Loading branch information
zachleat authored Jan 15, 2024
2 parents 8a5676a + 7203401 commit 8cdf2f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,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
// in their metadata which is actually the height of every single frame added together.
// In these cases, the metadata will contain an additional `pageHeight` property which
// is the height that the image should be displayed at.
metadata.height = metadata.pageHeight;
}

for(let outputFormat of outputFormats) {
if(!outputFormat || outputFormat === "auto") {
throw new Error("When using statsSync or statsByDimensionsSync, `formats: [null | auto]` to use the native image format is not supported.");
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ test("Animated gif", async t => {

t.is(stats.gif.length, 1);
t.is(stats.gif[0].width, 400);
t.is(stats.gif[0].height, 400);
// it’s a big boi
t.true( stats.gif[0].size > 1000*1000 );
});
Expand Down

0 comments on commit 8cdf2f7

Please sign in to comment.