Change the color of an SVG image file

function RecolorImage($ImageSvgFile, $ImageColor)
{
$FileContents = file_get_contents($ImageSvgFile);
$doc = new DOMDocument();
$dom->preserveWhiteSpace = False;
$doc->loadXML($FileContents) or die('Failed to load SVG file ' . $ImageSvgFile . ' as XML. It probably contains malformed data.');
$SvgTags = $doc->getElementsByTagName("svg");
if (preg_match('/^([0-9a-f]{1,2}){3}$/i', $ImageColor) == false)
{
die('Invalid color: ' . $ImageColor);
}
//Look at each element in the XML and add or replace it's Fill attribute to change the color.
$AllTags = $doc->getElementsByTagName("path");
foreach ($AllTags as $ATag)
{
$VectorColor = $ATag->getAttribute('fill');
if (strtoupper($VectorColor) != '#FFFFFF')
{
//This vector is not white, so change it's color.
$ATag->setAttribute('fill', '#' . $ImageColor);
$FileContents = $doc->saveXML($doc);
}
}
Return $FileContents;
}