【问题标题】:PHP Generated SVG as background imagePHP 生成的 SVG 作为背景图片
【发布时间】:2015-12-21 22:39:48
【问题描述】:

我想根据选择的背景颜色动态更改 SVG 的颜色。这是有效的,但它不能作为背景图像工作,因为文件名是 .php

这是我制作图像的 PHP 代码,我没有包含所有路径:

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

function adjustBridgtness($rgb, $steps) {
    //Only allow RGB value to be sent
    if (count($rgb) == 3) {
        //Negative darker, positive brighter
        $steps = max(-255, min(255, $steps));

        //New color
        $new_color = array();

        foreach($rgb as $color) {
            //$color = hexdec($color); 
            $color = max(0,min(255,$color + $steps));
            $new_color[] = $color;
        }

        return $new_color;
    }
}
//$colors[0] = array(67,189,151); //Row 1

$colors[0] = adjustBridgtness(array(67,189,151),0); //Row 1
$colors[1] = array(81,193,159); //Row 2
$colors[2] = array(95,199,165); //Row 3
$colors[3] = array(107,203,173); //Row 4
$colors[4] = array(127,211,185); //Row 5
$colors[5] = array(81,193,159); //Diagonal Lines
$colors[6] = array(127,211,185); //Verticle Line

?>
<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    width="796px" height="404px" viewBox="0 0 796 404">
    <path fillRule="evenodd" d="M 349.5 153.12C 349.5 153.12 396.88 200.5 396.88 200.5 396.88 200.5 349.5 247.88 349.5 247.88 349.5 247.88 302.12 200.5 302.12 200.5 302.12 200.5 349.5 153.12 349.5 153.12Z" fill="rgb(<?=$colors[0][0]?>,<?=$colors[0][1]?>,<?=$colors[0][2]?>)"/>
    <path fillRule="evenodd" d="M 251.5 153.12C 251.5 153.12 298.88 200.5 298.88 200.5 298.88 200.5 251.5 247.88 251.5 247.88 251.5 247.88 204.12 200.5 204.12 200.5 204.12 200.5 251.5 153.12 251.5 153.12Z" fill="rgb(<?=$colors[0][0]?>,<?=$colors[0][1]?>,<?=$colors[0][2]?>)"/>
</svg>

我只是把它放在 background-image 属性中,如下所示:

background-image:url(images/image.php);

这不起作用,因为它没有显示,我做错了什么?

附注只有网站管理员可以更改背景颜色,如果每次页面加载时生成SVG并在更改颜色时覆盖现有文件(例如background.svg)而不是生成颜色,会更好吗?

【问题讨论】:

  • 你试过在路径后关闭你的 svg 标签吗?
  • 已经关闭了,我只是没有把底部的代码全部贴出来,我现在添加它让大家知道它已经关闭了
  • 如果您只是浏览到 images/image.php 是否会显示?
  • 是的,只要我浏览它就可以了
  • 网址可以公开访问吗?

标签: php css svg


【解决方案1】:

嗯,它在这里工作......(至少对我来说)

&lt;div style="width:400px;height:400px;background-image:url(http://karl.uphero.com/images/background_banner1.php)"&gt;&lt;/div&gt;

【讨论】:

  • 嗯,我已将它链接到样式表中,而不是内联 incase,这会有所不同吗?
  • 嗯,它似乎正在工作,但它没有更新:S karl.uphero.com 在那里看到它,方块的顶部颜色与您在此处加载时不同:karl.uphero.com/images/background_banner1.php跨度>
  • 嗯,奇怪,现在似乎可以工作了。我认为它正在缓存......但现在当我刷新页面时它会重新加载。
猜你喜欢
  • 2012-02-29
  • 2015-03-13
  • 2014-03-24
  • 2021-12-08
  • 2013-01-11
  • 2018-06-18
  • 1970-01-01
  • 2011-11-28
相关资源
最近更新 更多