getfluid
3/9/2018 - 7:25 PM

Get SVG code from and uploaded SVG file

Get SVG code from and uploaded SVG file

/**
 * Get an uploaded SVG's code
 * 
 * @param  string $path The path to the uploaded SVG file
 * @return XML          The modified SVG code
 */
function get_svg($path) {
	$svg = file_get_contents(home_url() . $path);

	$dom = new DOMDocument();
	$dom->preserveWhiteSpace = false;
	$dom->loadXML($svg);

	if ($style = $dom->getElementsByTagName('style')->item(0)) {
		$style->parentNode->removeChild($style);
		$svg =  $dom->saveXML();
	}

	if ($title = $dom->getElementsByTagName('title')->item(0)) {
		$title->parentNode->removeChild($title);
		$svg =  $dom->saveXML();
	}

	if ($desc = $dom->getElementsByTagName('desc')->item(0)) {
		$desc->parentNode->removeChild($desc);
		$svg =  $dom->saveXML();
	}

	echo $svg;
}