cj333 cj333 - 1 year ago 189
PHP Question

php how to get width and height of a svg picture?

php how to get width and height of a svg picture? I tried to use

, but it failed.

I know SVG is
Scalable Vector Graphics
. But I find in
chrome's Review elements
, it can perfect get the Dimensions of a svg picture.

So I think maybe php can also do that job. Is anybody have a better idea? Thanks.


If it is difficult get the Dimensions, how to judge a svg picture which is
Horizontal picture
Vertical picture

Answer Source

The thing is: SVG images don't have a "size" in the sense you are probably thinking of. On the other hand, they DO have a height-to-width ratio.

This ratio can usually be found in the viewBox attribute.

If, on the other hand, the viewBox attribute is not present on the root SVG element, the image ratio is highly nontrivial to determine.


Side note: The reason Chrome gives you perfect coordinates isn't necessarily because it looks at the SVG to determine size; it could very well be a simple result of it setting the size.

Although the SVG element does have height and width attributes, these might not be specified as pixels, but any of a number of units, so they aren't necessarily a lot of help.