Skip to content

Instantly share code, notes, and snippets.

@blynx
Last active January 18, 2017 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blynx/d91d749e76c112f06a505a2d17dfdc2e to your computer and use it in GitHub Desktop.
Save blynx/d91d749e76c112f06a505a2d17dfdc2e to your computer and use it in GitHub Desktop.
PHP, ProcessWire: SVG placement function
/**
* placeSVG
*
* verson 1.0.4
*
* Insert svg file as svg markup or image tag
*
* @param String $filename file relative to templates folder
* @param array $option
* String markup 'img'|null, defines output
* String path alternate path
* String url alternate url
* @return String output String
*/
function placeSVG(String $filename, $option = ['markup' => null]) {
switch ($option['markup']) {
case 'img':
$baseUrl = isset($option['url']) ? $option['url'] : wire('config')->urls->templates;
$baseUrl .= substr($baseUrl, -1) === '/' ? '' : '/';
$filename = (strpos($filename, '/') === 0) ? substr($filename, 1) : $filename;
$url = $baseUrl.$filename;
return "<img src='$url'>";
break;
default:
$basePath = isset($option['path']) ? $option['path'] : wire('config')->paths->templates;
$basePath .= substr($basePath, -1) === '/' ? '' : '/';
$filename = (strpos($filename, '/') === 0) ? substr($filename, 1) : $filename;
$path = $basePath.$filename;
$svgContent = file_get_contents($path);
// strip doctype, xml definition, comments
$svgOutput = preg_replace("/(\<\?xml.+?\?\>)|(\<\!DOCTYPE.+?\>)|(\<\!--.+?\-\-\>)/", '', $svgContent);
return $svgOutput;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment