2012-07-09 31 views
5

我想用PHP创建一个SVG文件,然后将它包含在一个HTML文件中。这里是我到目前为止(以下this tutorial如何加载使用PHP生成的SVG文件?

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/。他们在同一个目录中。

+0

公共不公开 – Gntem 2012-07-09 19:32:22

+0

已经找到/已经修复,仍然有问题。 – SomeKittens 2012-07-09 19:33:00

+1

如果你删除行它呈现svg – Gntem 2012-07-09 19:40:50

回答

8

GeoPhoenix上述回答,我想我把它放到一个答案,这样有一个官方一个:

如果去掉<?xml?>线它呈现SVG

PHP可以看到<?作为PHP代码(而不是XML声明)的开始标记,这要归功于short-open-tag设置(thanks to fvu)。这是一个简单的办法,我刚刚更换

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

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

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

+0

这不是一个'修复',它的黑客.. – YemSalat 2015-11-25 00:54:13