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

How to resize animated webp/gif? #2434

Closed
luizbim opened this issue Nov 9, 2020 · 10 comments
Closed

How to resize animated webp/gif? #2434

luizbim opened this issue Nov 9, 2020 · 10 comments
Labels

Comments

@luizbim
Copy link

luizbim commented Nov 9, 2020

Hi guys, I'm trying to resize an animated webp but no success.
I saw today that some new API calls are being developed, but I thought that the animated resizing functionality already works in some way.

A little sample code:

      sharp("originalFilePath.webp", { animated: true })
        .resize({
          width: 200,
          height: 200,
        })
        .webp({
          pageHeight: 200,
          loop: 0,
          delay: 130,
          force: true,
        })
        .toFile("resizedFilePath.webp", (err, info) => {
          if (err) {
            console.error(err);
          }
         console.log('file resized')
        });

My objective is to resize and convert animated images back and forth using webp and gif extensions to accomplish a more cheaper bandwidth cost for my current project.

Thank you in advance for your time.

@lovell
Copy link
Owner

lovell commented Nov 10, 2020

Hi, if you're resizing n pages at once you'll need to set the height to a multiple of n:

  .resize({
-    width: 200,
+    width: 200 * n,
    height: 200,
  })

@luizbim
Copy link
Author

luizbim commented Nov 11, 2020

I've tried that, but the animation changed completely. The result is the first page moving from down to top.

@lovell
Copy link
Owner

lovell commented Nov 11, 2020

Sorry, I got width and height the wrong way round in the diff, it should be:

  .resize({
    width: 200,
-   height: 200,
+   height: 200 * n,
  })

@luizbim
Copy link
Author

luizbim commented Nov 11, 2020

Yeah, I've noticed that but the result is the same.

@lovell
Copy link
Owner

lovell commented Nov 11, 2020

You're probably seeing the effects of rounding "errors" at the top and bottom of each page/frame, which is pretty much why #2275 exists as a future possible enhancement.

Are you able to provide a sample input file? You'll probably only get any success when resizing to exactly 1/2 or 1/4 etc. of the original dimensions as things stand.

@luizbim
Copy link
Author

luizbim commented Nov 11, 2020

I've noticed that if the new size is below 50% the glitch appears. Above 50% the image is resized correctly.

The Image is here

@luizbim
Copy link
Author

luizbim commented Nov 12, 2020

Also, for some reason resized images are heavier than the original.
Original:

  • 512x512: 32k

Resized:

  • 512x512: 60k
  • 300x300: 92k
  • 256x256: 68k

@lovell
Copy link
Owner

lovell commented Dec 7, 2020

Please subscribe to #2275 for updates.

@lovell lovell closed this as completed Dec 7, 2020
@luizbim
Copy link
Author

luizbim commented Dec 7, 2020

Ok, thank you very much @lovell.

@KishorJena
Copy link

is the problem solved?

Repository owner locked and limited conversation to collaborators Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants