【发布时间】: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 是否会显示?
-
是的,只要我浏览它就可以了
-
网址可以公开访问吗?