douhui7136 2012-12-31 20:12
浏览 312
已采纳

如何从svg文件中提取高度和宽度

example:

<svg width="23" height="22" xmlns="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<path fill-opacity="0" id="svg_1" d="m23,2-20,9.5l22,9" stroke-width="4" stroke="#000000" fill="#ffffff" />
</g>
</svg>

I want to extract the width and height of the svg file so that I can do some positioning with canvg.

I know it's xml, but I can't figure it out. It's probably something really easy but it's the last day of the year and I can't figure it out.

Been trying:

PHP SimpleXML

$xml = simplexml_load_file($imageright) or die("Error: Cannot create object");
print_r($xml->getDocNamespaces());echo '</br>';

giving me Array ( [] => http://www.w3.org/2000/svg )

This also doesn't work:

foreach($xml->svg[0]->attributes() as $a => $b)
  {
  echo $a,'="',$b,"\"</br>";
  }

Any ideas?

  • 写回答

2条回答 默认 最新

  • douzachan4578 2012-12-31 20:17
    关注

    All you need is

    $xml = simplexml_load_file($imageright);
    $attr = $xml->attributes();
    printf("%s x %s", $attr->width, $attr->height);
    

    Output

    23 x 22
    

    Simple Online Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作