question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resize svg and output png file

See original GitHub issue

I am sorry if it is obvious or has been answered before, but this code

await sharp(new Buffer(layersWithSvg[0].icon_svg))
  .resize(100, 100)
  .png()
  .toFile('./assets/icon.png')

for icon_svg with metadata

{ format: 'svg',
  width: 16,
  height: 16,
  space: 'srgb',
  channels: 4,
  density: 72,
  hasProfile: false,
  hasAlpha: true }

outputs very poor quality png. Seems like it is scaling rasterized svg. How to scale svg and convert to png correctly?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
lovellcommented, Mar 25, 2021

Hello, by default the SVG will be rendered at a density of 72DPI, which the metadata for this image suggests will be 16x16. Then resize(100, 100) will resize the 16x16 to 100x100, hence the problem you’ve encountered.

You can set the density parameter on the constructor to increase the size of the initial rendering.

If a density of 72 renders a 16x16 image, then a density of at least 72*100/16 = 450 will be suitable for rendering a 100x100 image.

sharp(new Buffer(layersWithSvg[0].icon_svg), { density: 450 })...
1reaction
ReFruitycommented, Mar 7, 2017

@lovell Thanks again, it halved sprite generation time, you are the best.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resize SVG Online - Free SVG Resizer Tool - Pixelied
Resize SVG files online without editing their HTML code. With Pixelied, you can adjust the dimensions of SVG images in under a minute,...
Read more >
Free Online Resize SVG Images with High output Quality
Just upload your SVG image, choose a size and resampling type to get the resized image in desired format for free from any...
Read more >
Resize & Convert SVG to PNG (Online) with myScale - Hongkiat
Upload your .svg file. · Input the width in pixels (maintains the aspect ratio automatically). · Click “Generate” to preview the output, then ......
Read more >
How to resize a SVG image - Medium
Open the SVG file with your text editor. It should show lines of code as below. ... As you can see, width and...
Read more >
Resize many SVG vector images online for free - iLoveIMG
Resize SVG images for free. Change SVG dimensions in batch to optimize them for your website. Resizing SVG vectors maintaining the quality!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found