sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Enhancement: non-animated GIF output should default to no-loop

Open thatsmydoing opened this issue 1 year ago • 1 comments

Question about an existing feature

What are you trying to achieve?

When naively converting non-GIF to GIF, the output GIF is set to loop forever. It might make more sense to default to 1 instead.

When you searched for similar issues, what did you find that might be related?

I did not find anything related to loop forever.

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question

const sharp = require('sharp');

async function main() {
  await sharp(process.argv[2]).gif().toFile('out.gif');
  console.log(await sharp('out.gif').metadata());
}

main();

will have metadata loop: 0 when passed in a non-GIF but loop: 1 when passed in a non-animated GIF. In practice, they probably display the same but the output is technically an infinitely looping GIF with 1 frame. Tools like gifsicle also indicate "loop forever" on the output image.

Please provide sample image(s) that help explain this question

PNG input -> "animated" GIF output

non-animated GIF input -> non-animated GIF output

thatsmydoing avatar Sep 30 '22 15:09 thatsmydoing

As you've seen, loop is not set for non-animated images, so the output will either use the loop value from the input or default to -1, which is "loop forever".

We'll need to copy/set loop to 1 when the page count is also 1, somewhere around here:

https://github.com/lovell/sharp/blob/eacb8337fa0b868fcdffefd422acee4816fc428f/src/common.cc#L570-L571

lovell avatar Oct 01 '22 15:10 lovell