php 你如何在PHP中获得SVG图片的宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6532261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:39:31  来源:igfitidea点击:

How do you get the width and height of an SVG picture in PHP?

phpsvgdimensions

提问by cj333

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

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

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是“Scalable Vector Graphics”,但我发现Google Chrome的“Review elements”可以完美获取SVG图片的尺寸,所以我怀疑这在PHP中也是可能的。

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

如果很难获取尺寸,有什么方法可以判断SVG图片是垂直的还是水平的?

采纳答案by Williham Totland

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.

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

This ratio can usually be found in the viewBoxattribute.

这个比率通常可以在viewBox属性中找到。

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

另一方面,如果viewBox根 SVG 元素上不存在该属性,则很难确定图像比例。

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 settingthe size.

旁注:Chrome 为您提供完美坐标的原因并不一定是因为它查看 SVG 来确定大小;它很可能是它设置大小的简单结果。

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

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

回答by Brian

An SVG is simply an XML file, so the GD libs will not be of any help!

SVG 只是一个 XML 文件,所以 GD 库不会有任何帮助!

You should simply be able to parse the XML file to get such properties.

您应该能够简单地解析 XML 文件以获取此类属性。

$xml = '
<svg width="500" height="300" version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">

<rect x="90" y="10"
    width="400" height="280"
    style="fill: rgb(255,255,255); stroke: rgb(0,0,0); stroke-width: 1; " />
</svg>';

$xmlget = simplexml_load_string($xml);
$xmlattributes = $xmlget->attributes();
$width = (string) $xmlattributes->width; 
$height = (string) $xmlattributes->height;
print_r($width);
print_r($height);

The values need to be cast or they will return an object.

这些值需要强制转换,否则它们将返回一个对象。

回答by ausi

Regarding dimensions there are basically three different types of SVG images:

关于尺寸,基本上有三种不同类型的 SVG 图像:

  1. Fixed dimensions:Images that have widthand heightattributes. Chrome can show these dimensions perfectly and converts the specified units to pixels.

  2. Proportional dimensions:SVG images with a viewBoxattribute. Chrome shows the size of such images maximized to 300x150. So an image with a 16:9 ratio is shown as 267x150.

  3. No dimensions:SVG images without width, heightand viewBox. Chrome (and other browsers as well) use a default size of 300x150for such images.

  1. 固定尺寸:具有widthheight属性的图像。Chrome 可以完美地显示这些尺寸并将指定的单位转换为像素。

  2. 比例尺寸:具有viewBox属性的SVG 图像。Chrome 将此类图像的大小显示为最大化300x150。因此,比例为 16:9 的图像显示为267x150

  3. 无尺寸:SVG 图像没有width,heightviewBox。Chrome(以及其他浏览器)300x150对此类图像使用默认大小。

It is possible to do get the dimensions with PHP by reading the contents of the SVG image with PHPs DOM extensionand applying the rules from above. I wrote the PHP library contao/imagine-svgthat does exactly that and can be used as follows:

通过使用 PHP DOM 扩展读取 SVG 图像的内容并应用上面的规则,可以使用 PHP 获取尺寸。我编写了 PHP 库contao/imagine-svg,它就是这样做的,可以按如下方式使用:

$size = (new Contao\ImagineSvg\Imagine)
    ->open('/path/to/image.svg')
    ->getSize();
echo $size->getWidth();
echo $size->getHeight();

If you don't want to rely on a third party library, you can look at the source code of the getSize()methodto see how the library determines the dimensions.

如果不想依赖第三方库,可以查看方法源代码,getSize()看看库是如何确定维度的。

回答by Patricio Villarroel

I had the same question as I could not find an answer, invent and resolved as follows:

我有同样的问题,因为我找不到答案,发明并解决如下:

<?php
    $svgfile = simplexml_load_file("svgimage.svg");
    $width = substr($svgfile[width],0,-2);
    $height = substr($svgfile[height],0,-2);
?>

Note that the SVG file I used was created in Adobe Illustrator. So, the svg code of the image starts as follows:

请注意,我使用的 SVG 文件是在 Adob​​e Illustrator 中创建的。因此,图像的 svg 代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="400px" height="738px" viewBox="0 0 400 738" enable-background="new 0 0 400 738" xml:space="preserve">
...

Thanks to this, I could get width and height values.

多亏了这一点,我可以获得宽度和高度值。

And I use substrbecause $svgfile[width]returns the value with "px" suffix. I could also use viewBox value and split it to get the values.

我使用substr因为$svgfile[width]返回带有“px”后缀的值。我还可以使用 viewBox 值并将其拆分以获取值。

Greetings from Santiago, Chile

来自智利圣地亚哥的问候

回答by cronoklee

Here's a quick and dirtyhack 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.

这是使用正则表达式检查视图框大小的快速而肮脏的技巧。它在大多数情况下都有效,但请参阅其他答案以了解其局限性。

preg_match("#viewbox=[\"']\d* \d* (\d*) (\d*)#i", file_get_contents('file.svg'), $d);
$width = $d[1];
$height = $d[2];

回答by Hafenkranich

with simplexml_load_file

使用 simplexml_load_file

$svgXML = simplexml_load_file($imgUri);
list($originX, $originY, $relWidth, $relHeight) = explode(' ', $svgXML['viewBox']);

with preg_match regex(this one does for int and float values in viewBow values)

使用 preg_match 正则表达式(这个用于 viewBow 值中的 int 和 float 值)

preg_match("#viewbox=[\"']\d* \d* (\d*+(\.?+\d*)) (\d*+(\.?+\d*))#i", $file_get_contents($imgUri), $d);
$relWidth = (float) $d[1];
$relHeight = (float) $d[3];