php - 如何使用 php 更改 SVG 图像的填充?
问题描述
我目前正在尝试更改您使用 php 上传的任何 SVG 的填充。好吧,上传工作正常,但是我似乎找不到任何关于如何复制 SVG 的解决方案(我希望原始 SVG 保持不变)并使用 php.ini 更改填充属性。有人知道我可以使用哪些功能吗?
这是我的php代码:
<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);
?>
这是我的 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>
这是我第一次使用 xml 和我第二次使用 php,任何可以使代码更好的提示和更改都非常感谢!
编辑:这是我现在拥有的程序。它改变了 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>
我还更改了我试图编辑的 SVG,所以不要混淆。无论如何,这里还有代码:
<svg width="400" height="110" >
<rect
width="300"
height="100"
style="fill:rgb(111,111,111)"
/>
</svg>
解决方案
嗨,我有一个更好的解决方案。我认为这个库可以满足您的需求,而不是编写您自己的代码。它还将组织您的代码。
我只是解释一些代码如何使用
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
另请访问文档以获取更多详细信息。最好使用这个库。希望它会帮助你。
推荐阅读
- scala - 将 ZIO Stream 的输出写入文件
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/tmp/cats-v-dog
- reactjs - 让 VSCode 自动完成 React 道具类型和名称?
- r - 用于分组数据和 R 中带有日期的两个变量的 facet ggplot
- c# - 从不完全重复的对象列表中删除多余(重复)对象
- c++ - 如何在 C++ 中异步存储和弹出 uint8_t 从/到 uint32_t?
- java - 无法查看 JSP 文件中的数据
- angular - 用管子测试角分量
- terraform - Terraform 复杂变量定义
- .net-core - SD-WAN 中的 HttpClient