【问题标题】:How do I load a SVG file that's been generated with PHP?如何加载使用 PHP 生成的 SVG 文件?
【发布时间】:2012-07-09 05:34:18
【问题描述】:

我想用 PHP 创建一个 SVG 文件,然后将它包含在一个 HTML 文件中。这是我到目前为止所拥有的(关注this tutorial

svg.php:

<?php
header("Content-type: image/svg+xml");
?>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/
REC-SVG-20010904/DTD/svg10.dtd">

<svg width="310" height="140" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g style="stroke:black;fill:lightgreen" transform="translate(30,30)">
        <rect x="10" y="10" width="100" height="30" style="stroke-width:4"/>
        <circle cx="170" cy="25" r="20" style="stroke-width:4"/>
        <line x1="265" y1="10" x2="200" y2="70" style="stroke-width:4"/>
        <text x="80" y="90" style="font:size: 8">Basic shapes</text>
    </g>
</svg>

index.html

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html,body, #MOSVG { 
    height: 100%; 
    width: 100%;
    padding:0;
    margin:0;
}
</style>
</head>
<body>
    <object data="svg.php" type="image/svg+xml" id="MOSVG" />

</body>
</html>

当我查看index.html 时,我看到的只是一个空白页。控制台上没有警告/错误。 Apache 错误日志(我正在使用 XAMPP)给出[Mon Jul 09 14:36:15 2012] [error] [client 127.0.0.1] script 'C:/xampp/htdocs/svgtest/public/svg.php' not found or unable to stat, referer: http://localhost/svgtest/public/。它们在同一个目录中。

【问题讨论】:

  • 已找到/已修复,但问题仍然存在。
  • 如果你删除 行它会呈现 svg
  • 谢谢,成功了。为什么它设置为打破教程超出了我的范围......
  • 检查 short-open-tag 是否在 php.ini 中启用,已知它会导致 xml 输出出现奇怪的问题 - 另请参阅参考文档中提供的解决方法。

标签: php apache svg xampp


【解决方案1】:

GeoPhoenix 在上面回答了,我想我会把它放到一个答案中,以便有一个官方的答案:

如果您删除 &lt;?xml?&gt; 行,它会呈现 svg

由于short-open-tag 设置(thanks to fvu),PHP 可以将&lt;? 视为PHP 代码的开始标记(而不是XML 声明)。这是一个简单的修复,我刚刚替换了

<?xml version="1.0" encoding="iso-8859-1"?>

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>';?>

这具有相同的预期结果并且不会导致语法错误。

【讨论】:

    猜你喜欢
    • 2017-05-25
    • 2011-07-11
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2021-11-29
    • 2013-04-01
    相关资源
    最近更新 更多