sharp icon indicating copy to clipboard operation
sharp copied to clipboard

How to apply an overlay on each frame of a Gif

Open eraldoforgoli opened this issue 4 years ago • 4 comments

Question about an existing feature

How to apply an overlay on each frame of a gif?

What are you trying to achieve?

I'm trying to add a watermark to a gif. The issue is that the overlay/watermark is applied only on the first frame of the gif. When using the option: tile: true the watermark is repeated multiple times in each frame, as shown here: image

When I remove tile: true from the options, the overlay gets applied only to the first frame: image

I want to apply the overlay to each frame of the gif (on the bottom right, as shown in the image below, I've added a slack watermark for illustration purposes).

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

I viewed all the open issues, and most of them point to using tile: true, which only repeats the overlay infinitely on each screen. Example issues: https://github.com/lovell/sharp/issues/3122, https://github.com/lovell/sharp/issues/2680, https://github.com/lovell/sharp/issues/3121

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

const gifFile = fs.readFileSync(GIF_PATH);

  const response = await fetch(WATERMARK_URL);
  const watermarkBuffer = Buffer.from(await response.arrayBuffer());

  const watermark = await sharp(watermarkBuffer)
    .resize(WATERMARK_WIDTH, WATERMARK_HEIGHT)
    .toBuffer();

  const watermarkCompositeOptions = [
    {
      input: watermark,
      top: watermarkTopPosition,
      left: watermarkLeftPosition,
      tile: true,
    },
  ];
  const gifWithWatermark = await sharp(gifFile, { animated: true })
    .composite(watermarkCompositeOptions)
    .gif()
    .toBuffer();

  fs
    .createWriteStream(`/tmp/gif-${generateRandomId()}.gif`)
    .write(gifWithWatermark);

eraldoforgoli avatar Sep 02 '22 11:09 eraldoforgoli

Hi, you'll need to expand the dimensions of the tiled watermark overlay to match those of each page/frame.

lovell avatar Sep 09 '22 07:09 lovell

Hi, you'll need to expand the dimensions of the tiled watermark overlay to match those of each page/frame.

@lovell thanks for the reply ✋ So, in order for it to work, if the frame/gif is 600x600, the watermark has to be 600x60?

eraldoforgoli avatar Sep 09 '22 13:09 eraldoforgoli

Closing this issue since making the size of the watermark the same size as the gif worked (making the background transparent and placing the watermark at the bottom right).

eraldoforgoli avatar Sep 19 '22 10:09 eraldoforgoli

This detail about overlayed images needing to be the same size as the base gif really should be in the docs somewhere. As far as I can tell this issue is the only place on the internet where this is mentioned.

Arcanorum avatar Jun 30 '23 01:06 Arcanorum