动态SVG图像生成问题 [英] Problem with dynamic SVG image generation

查看:85
本文介绍了动态SVG图像生成问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个服务器端脚本(PHP),用于根据用户输入生成SVG图像。我使用以下代码:

 <?php 

echo'<?xml version =1.0standalone =no?>

<!DOCTYPE svg PUBLIC - // W3C // DTD SVG 1.1 // EN
http://www.w3.org/Graphics/SVG/1.1/DTD /svg11.dtd\">

< head>< meta http-equiv =Content-Typecontent =svg + xml/>< / head>

< svg width =100%height =100%version =1.1xmlns =http://www.w3.org/2000/svg>


< / svg>';

?>

我在某处读取MIME类型必须是svg + xml,所以我尝试将它设置为content-type就像你上面看到的那样。 Firefox正在接收正确的代码,但图像未呈现。根据维基百科上的SVG页面,SVG应该作为 image / svg + xml

另请参阅: 1.2 SVG MIME类型,文件扩展名和Macintosh文件类型



以下meta:

  < meta http-equiv =Content-Typecontent =svg / xml/> 

没有定义从服务器提供内容的方式 - 这更像是一种方式为HTML页面提供这些信息,当你无法定义它的服务方式时...

而且,我不确定meta元素是否在 SVG规范 - 我会让你检查^^





在这里,您需要做的是从您的服务器发送一个HTTP标头,指明您的数据的内容类型。



使用PHP header 函数;在你的情况下:

  header('Content-type:image / svg + xml'); 

echo'<?xml version =1.0standalone =no?>
<!DOCTYPE svg PUBLIC - // W3C // DTD SVG 1.1 // EN
http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd >
< svg width =100%height =100%version =1.1xmlns =http://www.w3.org/2000/svg>
< / svg>';

注意:


  • 我已经移除< meta> < head> 标签;不知道是否应该删除< head> ,但是,因为它是空的。

  • 我添加了调用函数

  • firefox显示SVG红色圆圈 - 因此,似乎工作正常; - ) / li>


希望这有助于您!


I am trying to write a server side script (PHP) for generating an SVG image based on user input. I am using the following code:

<?php

echo '<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<head><meta http-equiv="Content-Type" content="svg+xml" /></head>

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">

<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>

</svg>';

?>

I read somewhere that the MIME type must be svg+xml so I tried setting it in content-type as you can see above. The correct code is being received by Firefox but the image is not being rendered. Does somebody know what to change here?

解决方案

According to the SVG page on wikipedia, SVG should be served as image/svg+xml.
See also : 1.2 SVG MIME type, file name extension and Macintosh file type

The following meta :

<meta http-equiv="Content-Type" content="svg/xml" />

Doesn't define the way a content is served from a server -- it's more of a way to give that information, for HTML pages, when you can't define the way it's served...
And, I am not sure if the meta element is valid in the SVG specifications -- I'll let you check that ^^


What you need to do, here, is send a HTTP header from your server, indicating the content-type of your data.

This is done using the PHP header function ; in your case :

header('Content-type: image/svg+xml');

echo '<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
</svg>';

Note :

  • I have removed the <meta> and <head> tags ; not sure if the <head> should be removed, but, as it was empty....
  • I've added the call to the header function
  • The SVG red circle is properly displayed by firefox -- so, seems to work ;-)

Hope this helps !

这篇关于动态SVG图像生成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆