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

Inconsistent white border in different image sizes #3862

Closed
danielwerg opened this issue Nov 21, 2023 · 7 comments
Closed

Inconsistent white border in different image sizes #3862

danielwerg opened this issue Nov 21, 2023 · 7 comments
Labels

Comments

@danielwerg
Copy link

Question about an existing feature

What are you trying to achieve?

Consistent output without white border.

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

Nothing, not sure what to search for.

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

await sharp(svgPath).resize(1000).png().toFile(pngPath);
await sharp(svgPath).resize(1200).png().toFile(pngPath);
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
  <path fill="white" d="M32 32h300v300H32Z" />
  <path d="M32 32h300l-172 54L332 332H32Z" />
</svg>

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

1000x (no white border)

1000

1200x (white border)

1200

@lovell
Copy link
Owner

lovell commented Nov 21, 2023

Both these images appear to be rendered as-expected, both have a transparent background.

I suspect your question relates to the semi-transparent pixels around some of the edges in the 1200px PNG example.

The logic to calculate the "natural size" of an SVG is not entirely straightforward - see https://github.com/libvips/libvips/blob/35053f55e372bd3e687e987c2d68ea5cf62f85cd/libvips/foreign/svgload.c#L402 - and may result in scaling via cairo, which can introduce anti-aliasing at edges for some dimensions.

Given the viewBox in this SVG is relatively simple (e.g. no offset), providing explicit width and height attributes will help everything involved in its rendering to determine the natural size.

- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
+ <svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">

@danielwerg
Copy link
Author

Thank you for pointing me to the right direction. Unfortunately neither replacing viewBox with width and height nor simply adding it along viewBox did not change output for 1200x.

@lovell
Copy link
Owner

lovell commented Nov 26, 2023

You could also try using shape-rendering to reduce anti-aliasing.

- <path d="M32 32h300l-172 54L332 332H32Z" />
+ <path d="M32 32h300l-172 54L332 332H32Z" shape-rendering="crispEdges" />

@danielwerg
Copy link
Author

Doesn't really work in my case, it only made border slightly more obvious and made diagonal lines ugly (as expected).

1200x

1200x

@lovell
Copy link
Owner

lovell commented May 16, 2024

@danielwerg Were you able to make any progress with this?

@danielwerg
Copy link
Author

@lovell Nope, learned to accept it when I was still working on project that needed it.

@lovell
Copy link
Owner

lovell commented May 18, 2024

Thanks for the update, I'll close for now.

@lovell lovell closed this as completed May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants