我目前正在尝试更改您用php上传的任何SVG的填充。好了,上传工作正常,但是我似乎找不到关于如何复制SVG(我希望原始SVG保持不变)并使用php更改填充属性的任何解决方案。有谁知道我可以使用哪种功能?
这是我的php代码:
conda install -c https://conda.binstar.org/sstromberg pydot
这是我的SVG代码:
<form action="" method="post">
<p style="color:rgb(19, 107, 129);font-size:16px;margin:11px;">
SVG-File to re-color:
</p>
<input type="file" name="file" />
<p style="color:rgb(19, 107, 129);font-size:16px;margin:11px;">
Hex color that it should be:
</p>
<input type="text" name="clr" />
<input type="Submit" style="color:rgb(19, 107, 129);font-size:14px;" value="Send" />
<?php
if(isset($_POST['file'])){
$file = $_POST['file'];
}
if(isset($_POST['clr'])) {
$selected_color = $_POST['clr'];
}
$svgFile = file_get_contents($file, FILE_USE_INCLUDE_PATH);
$myXMLData = $svgFile;
$xml[] = simplexml_load_string($myXMLData) or die("Error: Cannot create object");
echo print_r($xml);
?>
这是我第一次使用xml,第二次使用php,感谢所有可以使代码更好的提示和更改!
编辑:这是我现在拥有的程序。它更改了SVG的填充属性,让用户下载它。
<svg width="400" height="110">
<rect width="300" height="100" />
<a>
style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)"
</a>
</svg>
我还更改了我尝试编辑的SVG,所以请不要感到困惑。无论如何,这里也是代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<form action="" method="post">
<p style="color:rgb(19, 107, 129);font-size:16px;margin:11px;">
SVG-File to re-color:
</p>
<input type="file" name="file"/>
<p style="color:rgb(19, 107, 129);font-size:16px;margin:11px;">
Hex color that it should be:
</p>
<input type="text" name="clr"/>
<input type="Submit"
value="Send"/>
<?php
if (isset($_POST['file'])) {
$file = $_POST['file'];
}
if (isset($_POST['clr'])) {
$selectedClr = $_POST['clr'];
}
$svgFile = file_get_contents($file);
$myXMLData = $svgFile;
$xml = simplexml_load_string($myXMLData) or die("Error: Cannot create
object");
echo print_r($xml);
$FileContents = $svgFile;
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($FileContents) or die('Failed to load SVG file ');
$rects = $doc->getElementsByTagName('rect');
foreach($rects as $rect) {
$rect->setAttribute('style', 'fill: ' . $selectedClr);
}
$finished = fopen("finishedSVG.xml", "w") or die("Unable to open
file!");
fwrite($finished, $doc->saveXML());
?>
<a href="http://127.0.0.1/Projekte/finishedSVG.xml" download>Download
Now</a>
答案 0 :(得分:0)
嗨,我对此有更好的解决方案。我认为该库可以满足您的需求,而不是编写自己的代码。它还可以组织您的代码。
我只是解释一些代码如何使用
use SVG\SVG; //import
use SVG\Nodes\Shapes\SVGRect; //import
$square = new SVGRect(0, 0, 50, 50);
$square->setStyle('fill', '#CCCCCC');
it's gonna fill this by grey colour
header('Content-Type: image/svg+xml');
echo $square; // it will echo your image
有关更多详细信息,请访问documentation。最好使用此库。希望对您有帮助。