【问题标题】:PHP SimpleXML add CSS to SVGPHP SimpleXML 添加 CSS 到 SVG
【发布时间】:2019-01-30 12:50:52
【问题描述】:

我正在尝试在现有 svg 中添加一个 def 元素,但插入的节点数据是一个字符串,而不是像这样的 css 标记(包含“&lt\;”,整个节点值用双引号括起来):

"<defs xmlns="http://www.w3.org/2000/svg">&lt;style type="text/css"&gt\;@font-face {font-family: DaimlerCS-Demi;src: url('../fonts/CorporateS/CorporateS-Demi.otf');}&lt\;/style&gt;</defs>"

我们如何正确地为 svg 中的 defs 添加一个 css 节点?这是我现有的 php 代码:

$data = trim(file_get_contents('php://input'));
$svg = new SimpleXMLElement($data);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$defContent = <<<EOF
<style type="text/css">@font-face {font-family: DaimlerCS-Demi;src: 
url('../fonts/CorporateS/CorporateS-Demi.otf');}</style>
EOF;
$def = $svg->addChild("defs", $defContent);
$svg = $svg->asXML();
$filePath = "./res/" . base64_encode(time()) . ".svg";
file_put_contents($filePath, $svg);

另外,如果可能的话,我们如何将节点添加为第一个子节点,而不是作为最后一个子节点插入

【问题讨论】:

    标签: php svg simplexml


    【解决方案1】:

    我必须像添加 def 一样添加样式节点:

    $def = $svg->addChild('def');
    $style = $def->addChild('style', $defContent);
    $style->addAttribute('type', 'text/css');
    

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多