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

如何从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 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死