如何在 PHP 中获取 SVG 图片的宽度和高度?
我尝试在 SVG 文件上使用 getimagesize()
,但失败了。
我知道SVG是“Scalable Vector Graphics”,但我发现Google Chrome的“Review elements”可以完美地获取SVG图片的尺寸,所以我怀疑这在PHP中也是可能的。
如果很难获取尺寸,有没有办法判断SVG图片是垂直还是水平?
I tried to use getimagesize()
on an SVG file, but it failed.
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.
If it is difficult to get the dimensions, is there any way to judge whether an SVG picture is vertical or horizontal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是:SVG 图像没有您可能想到的意义上的“大小”。另一方面,它们确实具有高宽比。
这个比例通常可以在
viewBox
属性。另一方面,如果根 SVG 元素上不存在
viewBox
属性,则确定图像比例非常重要。编辑:
旁注:Chrome 为您提供完美坐标的原因不一定是因为它查看 SVG 来确定大小;而是因为它会根据 SVG 来确定大小。它很可能是设置大小的简单结果。
尽管 SVG 元素确实具有
height
和width
属性,但这些属性可能不会指定为像素,而是指定为多个单位中的任意一个,因此它们不一定是很多单位帮助。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.Edit:
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
andwidth
attributes, these might not be specified as pixels, but any of a number of units, so they aren't necessarily a lot of help.SVG 只是一个 XML 文件,因此 GD 库不会有任何帮助!
您应该能够简单地解析 XML 文件来获取此类属性。
这些值需要进行转换,否则它们将返回一个对象。
An SVG is simply an XML file, so the GD libs will not be of any help!
You should simply be able to parse the XML file to get such properties.
The values need to be cast or they will return an object.
关于尺寸,SVG 图像基本上分为三种不同类型:
固定尺寸:具有
宽度
和高度
属性的图像。 Chrome 可以完美地显示这些尺寸,并将指定的单位转换为像素。比例尺寸:具有
viewBox
属性的 SVG 图像。 Chrome 将此类图像的尺寸最大化为300x150
。因此,比例为 16:9 的图像显示为267x150
。无尺寸:没有
宽度
、高度
和viewBox
的SVG图像。 Chrome(以及其他浏览器)对此类图像使用默认尺寸300x150
。可以通过使用 PHP 读取 SVG 图像的内容来获取 PHP 的尺寸 DOM扩展并应用上面的规则。我编写了 PHP 库 contao/imagine-svg ,它正是这样做的,并且可以按如下方式使用:
如果你不想依赖第三方库,你可以看看
getSize() 的源代码
方法 查看库如何确定尺寸。Regarding dimensions there are basically three different types of SVG images:
Fixed dimensions: Images that have
width
andheight
attributes. Chrome can show these dimensions perfectly and converts the specified units to pixels.Proportional dimensions: SVG images with a
viewBox
attribute. Chrome shows the size of such images maximized to300x150
. So an image with a 16:9 ratio is shown as267x150
.No dimensions: SVG images without
width
,height
andviewBox
. Chrome (and other browsers as well) use a default size of300x150
for such images.It is possible to do get the dimensions with PHP by reading the contents of the SVG image with PHPs DOM extension and applying the rules from above. I wrote the PHP library contao/imagine-svg that does exactly that and can be used as follows:
If you don’t want to rely on a third party library, you can look at the source code of the
getSize()
method to see how the library determines the dimensions.我有同样的问题,因为我找不到答案,发明并解决如下:
请注意,我使用的 SVG 文件是在 Adobe Illustrator 中创建的。因此,图像的 SVG 代码如下所示:
多亏了这个,我可以获得宽度和高度值。
我使用
substr
因为$svgfile[width]
返回带有“px”后缀的值。我还可以使用 viewBox 值并将其拆分以获取值。来自智利圣地亚哥的问候
I had the same question as I could not find an answer, invent and resolved as follows:
Note that the SVG file I used was created in Adobe Illustrator. So, the SVG code of the image starts as follows:
Thanks to this, I could get width and height values.
And I use
substr
because$svgfile[width]
returns the value with "px" suffix. I could also use viewBox value and split it to get the values.Greetings from Santiago, Chile
这是一个使用正则表达式检查视图框大小的快速而肮脏的技巧。它在大多数情况下都有效,但请参阅其他答案以了解其局限性。
Here's a quick and dirty hack to check the viewbox size with regex. It works in most cases but do see the other answers to get an idea of its limitations.
使用 simplexml_load_file
使用 preg_match 正则表达式
(这适用于 viewBow 值中的 int 和 float 值)
with simplexml_load_file
with preg_match regex
(this one does for int and float values in viewBow values)