public function getInlineSvg($filename, $params = [])
    {
        $fullPath = $this->assetDir.$filename;

        if (!file_exists($fullPath)) {
            throw new \Exception(sprintf('Cannot find svg file: "%s"', $fullPath));
        }

        $svgString = file_get_contents($fullPath);

        $hasAttr = array_key_exists('attr', $params);
        $hasClasses = array_key_exists('classes', $params);
        if ($hasAttr || $hasClasses) {
            $svg = simplexml_load_string($svgString);
            $attrs = $svg->attributes();
        }
        if ($hasAttr) {
            foreach ($params['attr'] as $key => $value) {
                if ($attrs[$key]) {
                    $attrs[$key] = $value;
                } else {
                    $svg->addAttribute($key, $value);
                }
            }
            $svgString = $svg->asXML();
        }
        if ($hasClasses) {
            if ($attrs['class']) {
                $attrs['class'] .= ' '.$params['classes'];
            } else {
                $svg->addAttribute('class', $params['classes']);
            }
            $svgString = $svg->asXML();
        }

        // remove annoying xml version added by as XML method
        $svgString = preg_replace("#<\?xml version.*?>#", '', $svgString);

        // remove comments
        $svgString = preg_replace('#<!--.*-->#', '', $svgString);

        return $svgString;
    }
public function getInlineSvg($filename, $params = [])
    {
        $fullPath = $this->assetDir.$filename;

        if (!file_exists($fullPath)) {
            throw new \Exception(sprintf('Cannot find svg file: "%s"', $fullPath));
        }

        $svgString = file_get_contents($fullPath);

        $hasAttr = array_key_exists('attr', $params);
        $hasClasses = array_key_exists('classes', $params);
        if ($hasAttr || $hasClasses) {
            $svg = simplexml_load_string($svgString);
            $attrs = $svg->attributes();
        }
        if ($hasAttr) {
            foreach ($params['attr'] as $key => $value) {
                if ($attrs[$key]) {
                    $attrs[$key] = $value;
                } else {
                    $svg->addAttribute($key, $value);
                }
            }
            $svgString = $svg->asXML();
        }
        if ($hasClasses) {
            if ($attrs['class']) {
                $attrs['class'] .= ' '.$params['classes'];
            } else {
                $svg->addAttribute('class', $params['classes']);
            }
            $svgString = $svg->asXML();
        }

        // remove annoying xml version added by as XML method
        $svgString = preg_replace("#<\?xml version.*?>#", '', $svgString);

        // remove comments
        $svgString = preg_replace('#<!--.*-->#', '', $svgString);

        return $svgString;
    }
public function getInlineSvg ( $ filename , $ params = [ ] ) { $ fullPath = $ this -> assetDir . $ filename ; if ( ! file_exists ( $ fullPath ) ) { throw new \ Exception ( sprintf ( 'Cannot find svg file: "%s"' , $ fullPath ) ) ; } $ svgString = file_get_contents ( $ fullPath ) ; $ hasAttr = array_key_exists ( 'attr' , $ params ) ; $ hasClasses = array_key_exists ( 'classes' , $ params ) ; if ( $ hasAttr || $ hasClasses ) { $ svg = simplexml_load_string ( $ svgString ) ; $ attrs = $ svg -> attributes ( ) ; } if ( $ hasAttr ) { foreach ( $ params [ 'attr' ] as $ key => $ value ) { if ( $ attrs [ $ key ] ) { $ attrs [ $ key ] = $ value ; } else { $ svg -> addAttribute ( $ key , $ value ) ; } } $ svgString = $ svg -> asXML ( ) ; } if ( $ hasClasses ) { if ( $ attrs [ 'class' ] ) { $ attrs [ 'class' ] .= ' ' . $ params [ 'classes' ] ; } else { $ svg -> addAttribute ( 'class' , $ params [ 'classes' ] ) ; } $ svgString = $ svg -> asXML ( ) ; } // remove annoying xml version added by as XML method $ svgString = preg_replace ( "#<\?xml version.*?>#" , '' , $ svgString ) ; // remove comments $ svgString = preg_replace ( '#<!--.*-->#' , '' , $ svgString ) ; return $ svgString ; }
Get an inline svg.

@param string $filename Path to the svg file
@param array  $params   Optional parameters

@return string

@throws \Exception
Get an inline svg .