SVG - 在组中包裹元素

时间:2012-05-01 15:07:34

标签: php xml svg

想要将一组svg元素包装为新组。问题是新组没有关闭标记。如何将它包装到新组中,此时它将被添加到最后。

php代码:

<?php 
$lagen = array();

$dom = new DOMDocument;

$dom->load("00000000001.svg"); 

$source= $dom->getElementsByTagName("g")->item(0); 

$newdom = new DOMDocument;

$newdom->formatOutput = true;

$svg = $newdom->createElement("svg");

$newdom->appendChild($svg); 

$root = $newdom->createElement("g");

$root->setAttribute("id", "dcroot");

$root->setAttribute("display", "visible");

$newdom->documentElement->appendChild($source); 

$node = $newdom->importNode($parent, true);

$newdom->documentElement->appendChild($node);

echo $newdom->save("test.svg");
?> 

原始svg:

<g id="laag">
<path d="M363.92 496.92L363.92 496.31 363.66 496.46 363.92 496.92Z" fill-rule="evenodd" fill="gray"/>
<path d="M363.92 496.92L386.43 535.88 363.92 496.31 363.92 496.92Z" fill-rule="evenodd" fill="gray"/>
<path d="M386.43 535.88L386.43 483.3 363.92 496.31 386.43 535.88Z" fill-rule="evenodd" fill="gray"/>
<path d="M386.43 535.88L470.68 681.69 386.43 483.3 386.43 535.88Z" fill-rule="evenodd" fill="gray"/>
<path d="M470.68 681.69L470.68 629.12 386.43 483.3 470.68 681.69Z" fill-rule="evenodd" fill="gray"/>
<path d="M470.68 681.69L493.45 668.53 470.68 629.12 470.68 681.69Z" fill-rule="evenodd" fill="gray"/>
</g>

输出我需要的东西:

<g id="wrapper">
<g id="laag">
<path d="M363.92 496.92L363.92 496.31 363.66 496.46 363.92 496.92Z" fill-rule="evenodd" fill="gray"/>
<path d="M363.92 496.92L386.43 535.88 363.92 496.31 363.92 496.92Z" fill-rule="evenodd" fill="gray"/>
<path d="M386.43 535.88L386.43 483.3 363.92 496.31 386.43 535.88Z" fill-rule="evenodd" fill="gray"/>
<path d="M386.43 535.88L470.68 681.69 386.43 483.3 386.43 535.88Z" fill-rule="evenodd" fill="gray"/>
<path d="M470.68 681.69L470.68 629.12 386.43 483.3 470.68 681.69Z" fill-rule="evenodd" fill="gray"/>
<path d="M470.68 681.69L493.45 668.53 470.68 629.12 470.68 681.69Z" fill-rule="evenodd" fill="gray"/>
</g>
</g>

0 个答案:

没有答案