如何加载一个已经用PHP生成的SVG文件? [英] How do I load a SVG file that's been generated with PHP?
问题描述
我想用PHP创建一个SVG文件,然后将其包含在HTML文件。这里是我到目前为止(以下本教程)
I want to create a SVG file with PHP, and then include it in a HTML file. Here's what I have so far (following 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)给 [周一七月09十四点36分15秒2012] [错误] [客户端127.0.0.1]脚本'C:/ XAMPP / htdocs中/ svgtest /公/ svg.php未找到或无法Stat预测,引用者:HTTP://本地主机/ svgtest /公/
。他们在同一目录下。
When I look at index.html
all I see is a blank page. There's no warnings/errors on the console. Apache error logs (I'm using XAMPP) give [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/
. They're in the same directory.
推荐答案
GeoPhoenix上述回答,我想我把它放到一个答案,这样有一个官方之一:
GeoPhoenix answered above, I figured I'd put it into an answer so that there's an offical one:
如果您删除&LT;?XML&GT;
行它呈现SVG
if you remove the
<?xml?>
line it renders the svg
PHP可以看到&LT;
作为PHP code中的开始标记(而不是一个XML声明),这要感谢的短开放标记设置(<一个href=\"http://stackoverflow.com/questions/11401500/how-do-i-load-a-svg-file-thats-been-generated-with-php#comment15034606_11401500\">thanks到FVU )。这是一个简单的办法,我刚刚更换
PHP can see <?
as the opening tag for PHP code (rather than an XML declaration), thanks to the short-open-tag setting (thanks to fvu). This is an easy fix, I just replaced
<?xml version="1.0" encoding="iso-8859-1"?>
与
<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>';?>
此具有相同的预期的效果,不会引起语法错误。
This has the same intended result and doesn't cause syntax errors.
这篇关于如何加载一个已经用PHP生成的SVG文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!