簡體   English   中英

Symfony - 返回沒有 html 或正文標簽的 svg 標記

[英]Symfony - Return svg markup without html or body tags

美好的一天社區,

我現在對 symfony controller 有點問題。 我想動態創建一個 svg 並在我的網頁上僅顯示 svg 的標記。

像這樣:

<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="40" height="40"><rect x="0" y="0" width="40" height="40" style="fill: #0000FF" /></svg>

但是當我嘗試返回我的 svg 時,我總是在響應中得到 html 和正文標簽:

<!--?xml version="1.0" encoding="utf-8"?--><html><head></head><body><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="40" height="40"><rect x="0" y="0" width="40" height="40" style="fill: #0000FF"></rect></svg></body></html>

在我的 Controller 中,我通過以下包生成 svg: https://github.com/meyfa/php-svg

$image = new SVG(40,40);
        $doc = $image->getDocument();
        $square = new SVGRect(0, 0, 40,40);
        $square->setStyle('fill', '#0000FF');
        $doc->addChild($square);

        return new Response($image->toXMLString());

我的目標是只在頁面上顯示 svg 以便我可以在另一個頁面上使用生成的圖像,如下所示:

<img src="https://mypage/mygeneratedsvg">

有人對我有任何想法或意見嗎?

在發布問題后,我得出了結論。 就是這么簡單。。

我必須像這樣在標題中定義 Content-Type:

return new Response($image->toXMLString(), 200, ['Content-Type' => 'image/svg+xml']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM