domenic / svg2png

Converts SVGs to PNGs, using PhantomJS
Other
585 stars 134 forks source link

Amended getSVGDimensions for when size only fetchable from viewbox #83

Closed pgolding closed 7 years ago

pgolding commented 7 years ago

It seems that SVG files exported from Adobe Illustrator do not contain width or height attributes and rely only on viewbox dimensions. Although I thought from your docs that you include this use case, it seems not so I added a simple condition to capture it. Now works fine - I tested on a number of files exported from AI.

domenic commented 7 years ago

This is working as intended. You must supply an explicit width and/or height for such files.

pgolding commented 7 years ago

Okay, but that info will only be grepped from the SVG element anyway, so why not include that use case in the code when grabbing the SVG element data? It is because it's not valid SVG?

domenic commented 7 years ago

It's because viewBox signifies aspect ratio, not size. If you open a viewBox-only SVG in a typical image viewer (and, in all browsers) it will expand to fill the window, only using the viewBox for aspect ratio. That's the same behavior svg2png exhibits, but since we don't know how big your screen is, we need you to manually provide it.

pgolding commented 7 years ago

Yeah, that's what I assumed, but when I grabbed the viewBox element from within the console it was set at a constant (original) size not matter the browser scaling. Ditto when dumping the value back out from Phantom. It seems that the scaling to window container is via modification to the width/height attributes (added by the UA if not present in the code). It seems that the viewBox is how much of the SVG path bounding box can be viewed within the container.

However, I am not an SVG expert by any means and don't wish to muddle your code. Thanks for your help and your lib. I'll just use my own hack. Cheers.