vierwd / svg-inliner
PHP项目内联SVG的实用函数
1.4.1
2022-11-09 16:45 UTC
Requires
- php: ^7.4.0 | ^8.0 | ^8.1
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.0
- friendsofphp/php-cs-fixer: ^2.12
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.1.0
- phpunit/phpunit: ^8.5 | ^9.5
- vierwd/coding-standard: ^1.3.0
README
PHP项目内联SVG的实用函数
composer require 'vierwd/svg-inliner'
此库可用于在PHP项目中内联SVG。它处理SVG并将一些内容进行更改
fill="transparent"
应该是fill="none"
- 在IE 9中短闭合标签可能会有问题。这就是为什么
<path d="..." />
变成<path d="..."></path>
。 - 注释被移除
- 在具有文件名作为类名的SVG元素上添加class属性
还可以组合SVG并只输出 <use>
标签,这些标签引用SVG。如果SVG被多次使用,这很有意义。但你会失去对每个标签单独样式的功能。
使用方法
$svgInliner = new SvgInliner($defaultOptions); // render a single SVG file and output a <use>-tag $output = $svgInliner->renderSVGFile($pathToSvgFile, $options); // render all SVGs which are references by <use>-tags $allSVGs = $svgInliner->renderFullSVG(); // render a single SVG and output directly $output = $svgInliner->renderSVGFile($pathToSvgFile, ['excludeFromConcatenation' => true]);
选项
-
excludeFromConcatenation
: 直接输出SVG而不是<use>
-标签。在构造函数中作为默认选项可用。默认为false。 -
ignoreDuplicateIds
: 如果两个SVG使用相同的id属性,不要抛出异常。在构造函数中作为默认选项可用。默认为false。 -
removeComments
: 移除所有XML注释。在构造函数中作为默认选项可用。默认为true。 -
identifier
: SVG的标识符。如果使用相同的标识符,则不会再次加载文件,而是从缓存中使用。默认为小写文件名(特殊字符转换为破折号。) -
width
: SVG的宽度属性。默认为none,并将属性保持不变。 -
height
: SVG的高度属性。默认为none,并将属性保持不变。 -
class
: SVG标签的附加类。默认添加.svg
和.svg-identifier
。 -
external
: 将SVG作为外部<use>
-标签嵌入。 -
url
: 外部SVG的URL
外部SVG
可以包含SVG作为指向外部SVG文件的链接
$svgInliner = new SvgInliner($defaultOptions); // render a single SVG file and output a <use>-tag with an external file $output = $svgInliner->renderSVGFile($pathToSvgFile, [ 'external' => true, 'url' => '/static/test.svg', ]);
输出
<svg width="50" height="50" viewBox="0 0 50 50"> <use xlink:href="/static/test.svg?912ec803#main" width="50" height="50" /> </svg>
这允许使用CSS样式化SVG的某些部分,但允许SVG仍然可缓存。您可以使用 fill
和 stroke
为SVG的所有没有填充或描边的部分设置颜色。您可以使用 color
设置在SVG内部用作 currentColor
的颜色。
注意
- IE 11不支持在
<use>
-标签中使用外部SVG。如果您需要IE 11支持,则需要使用polyfill如 svgxuse。 - SVG需要一个具有
id
属性的标签。如果url
选项不包含#hash
,SvgInliner将自动使用SVG中找到的第一个ID。如果没有找到ID,将抛出异常。