如何在PHP中获得SVG图片的宽度和高度? [英] How do you get the width and height of an SVG picture in PHP?

查看:143
本文介绍了如何在PHP中获得SVG图片的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在SVG文件上使用 getimagesize(),但是失败了。

I tried to use getimagesize() on an SVG file, but it failed.

我知道SVG是可缩放矢量图形,但是我发现Google Chrome的审阅元素可以完美地获取SVG图片的尺寸,因此我怀疑在PHP中也可以做到这一点。

I know that SVG is "Scalable Vector Graphics", but I find that Google Chrome’s "Review elements" can perfectly get the dimensions of an SVG picture, so I suspect that this is also possible in PHP.

如果难以获取尺寸,是否可以判断SVG图片是垂直还是水平?

If it is difficult to get the dimensions, is there any way to judge whether an SVG picture is vertical or horizontal?

推荐答案

问题是:SVG图像在您可能想到的意义上没有大小。另一方面,它们确实具有高宽比。

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.

通常可以在 viewBox 属性。

This ratio can usually be found in the viewBox attribute.

另一方面,如果根SVG元素上没有 viewBox 属性,则确定图像的比例非常重要。

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

编辑:

侧面说明:Chrome提供完美坐标的原因不一定是因为它查看SVG以确定大小;

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.

尽管SVG元素确实具有 height width 属性,这些属性可能未指定为像素,而是许多单位中的任何一个,因此它们不一定有很多帮助。

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.

这篇关于如何在PHP中获得SVG图片的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆