yassinedoghri / php-icons
基于iconify API的PHP库,用于下载和渲染来自流行开源图标集的svg图标。
Requires
- php: >=8.1
- composer-runtime-api: ^2.2
- adhocore/cli: ^1.7.2
- nikic/php-parser: ^4.19.2
Requires (Dev)
- kint-php/kint: ^5.1.1
- pestphp/pest: ^2.35.1
- pestphp/pest-plugin-type-coverage: ^2.8.6
- phpstan/phpstan: ^1.12.4
- rector/rector: ^1.2.5
- symplify/coding-standard: ^12.2.3
- symplify/easy-coding-standard: ^12.3.5
This package is auto-updated.
Last update: 2024-09-29 17:08:41 UTC
README
PHPIcons 🐘 🙂
一个方便的PHP库,用于渲染svg图标。
直接从您的php文件中访问超过200,000个图标,这些图标来自超过150个开源图标集!
感谢 Iconify ❤️
🧩 集成
🚀 开始使用
1. 通过composer安装
composer require yassinedoghri/php-icons
2. 配置
运行以下命令以初始化配置文件
vendor/bin/php-icons init
这将提示您在项目的根目录中创建一个 php-icons.php
配置文件。有关更多信息,请参阅 配置参考。
3. 在任何地方使用
3.1. icon(string $iconKey)
方法
在视图文件中使用 icon
方法,并将图标键字符串 ({prefix}:{icon}
) 作为参数
<?php use PHPIcons\PHPIcons; $phpicons = new PHPIcons('/path/to/config/file.php'); echo $phpicons->icon('material-symbols:bolt'); // <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"> // <path fill="currentColor" d="m8 22l1-7H4l9-13h2l-1 8h6L10 22z"/> // </svg>
👉 使用 attr()
或 attributes()
方法添加任何属性
echo $phpicons ->icon('material-symbols:bolt') ->attr('class', 'text-2xl') ->attr('style', 'color: yellow;'); // <svg class="text-2xl" style="color: yellow;" […]>…</svg> echo $phpicons ->icon('material-symbols:bolt') ->attributes([ 'class' => 'text-2xl', 'style' => 'color: yellow;' ]); // <svg class="text-2xl" style="color: yellow;" […]>…</svg>
提示
从 Iconify的索引 中查找并复制流行开源图标集的图标键。
3.2. 扫描源文件并加载图标
重要
首次定义图标时,将显示占位符(默认为�
)。
请确保运行scan
命令以加载SVG文件。
vendor/bin/php-icons scan
⚙️ 配置参考
您的配置文件同时由 php-icons
CLI工具和PHPIcons类使用,其外观如下
<?php declare(strict_types=1); use PHPIcons\Config\PHPIconsConfig; return PHPIconsConfig::configure() ->withPaths([ __DIR__ . '/src' ]) ->withDefaultPrefix('') ->withPlaceholder('�');
路径
withPaths([])
源文件路径列表。PHP文件将被解析并扫描以发现您定义的图标。
API主机
withAPIHosts([])
Iconify API 主机,用于查询下载svg图标。首先查询第一个主机,其余用作备份。
默认为Iconify的公共主机:["https://api.iconify.design","https://api.simplesvg.com", "https://api.unisvg.com"]
本地图标集
withLocalIconSets([])
如果您有自定义图标,php-icons可以在您的文件系统中本地查找,而不是调用Iconify API。
重要
php-icons将在您的本地图标集中查找{name}.svg
文件
接受一个关联数组,其中图标集前缀作为键,路径作为值。
示例
my-custom-set/
├── heart.svg
├── rocket.svg
├── star.svg
└── user.svg
// in your config file ->withLocalIconSets([ 'custom' => '/path/to/my-custom-set', ])
// ✅ ALL GOOD echo $phpicons->icon('custom:heart'); echo $phpicons->icon('custom:rocket'); echo $phpicons->icon('custom:star'); echo $phpicons->icon('custom:user'); // ❌ ICONS NOT FOUND echo $phpicons->icon('custom:banana'); echo $phpicons->icon('custom:key');
默认前缀
withDefaultPrefix('')
默认图标集前缀,当未设置时使用。
示例
使用 material-symbols
设置为默认前缀
// this echo $phpicons->icon('bolt'); // same as this echo $phpicons->icon('material-symbols:bolt');
默认图标
withDefaultIcon()
当未找到图标时使用的默认图标。
接受一个图标键 {prefix}:{name}
。如果没有设置前缀,则使用默认前缀。
每个集合的默认图标
withDefaultIconPerSet([])
当在集合中未找到图标时使用的默认图标。
接受一个关联数组,其中键是图标集前缀,值是默认图标。
占位符
withPlaceholder('�')
当图标找不到或未知时显示的字符串。
默认为 �
(替换字符)。
标识符
withIdentifiers([])
用于在源文件中匹配图标键的函数或方法名称。
默认为 ['icon']
。
🖥️ 命令行命令
> vendor/bin/php-icons _ _ _ __ | |__ _ __ (_) ___ ___ _ __ ___ | '_ \| '_ \| '_ \ | |/ __/ _ \| '_ \/ __| | |_) | | | | |_) | | | (_| (_) | | | \__ \ | .__/|_| |_| .__/ |_|\___\___/|_| |_|___/ |_| |_| A convenient PHP library to render svg icons ---------------------------------------------- PHPIcons, version 1.0.0.0-dev Commands: * init i Configure PHPIcons interactively scan s Scans source files and loads icons Run `<command> --help` for specific help
❤️ 致谢
没有 Iconify 团队和维护 众多开源图标集 的设计师们的出色工作,这一切将不可能实现。
灵感来源于 astro-icon、blade-icons 和 rector。
📜 许可证
代码在 MIT 许可证 下发布。
版权所有(c)2024-至今,Yassine Doghri (@yassinedoghri).