Eine praktische PHP-Bibliothek zum Rendern von SVG-Symbolen.
Erhalten Sie Zugriff auf über 200.000 Symbole aus mehr als 150 Open-Source-Symbolsätzen direkt aus Ihren PHP-Dateien!
Danke an Iconify ❤️
composer require yassinedoghri/php-icons
Führen Sie den folgenden Befehl aus, um die Konfigurationsdatei zu initialisieren:
vendor/bin/php-icons init
Dadurch werden Sie aufgefordert, eine Konfigurationsdatei php-icons.php
im Stammverzeichnis Ihres Projekts zu erstellen. Weitere Informationen finden Sie in der Konfigurationsreferenz.
icon(string $iconKey, array $attributes)
Funktion Verwenden Sie in Ihren Ansichtsdateien die Funktion „Global icon(…)
mit dem Symbolschlüssel ( {prefix}:{icon}
) als Parameter:
{prefix}
: ist das Präfix des Symbolsatzes{name}
: ist der Symbolname echo 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>
Um Attribute hinzuzufügen, verwenden Sie den zweiten Parameter oder rufen Sie die Methoden attr()
oder attributes()
auf:
echo icon ( ' material-symbols:bolt ' , [
' class ' => ' text-2xl ' ,
' style ' => ' color: yellow; '
]);
// <svg class="text-2xl" style="color: yellow;" […]>…</svg>
echo icon ( ' material-symbols:bolt ' )
-> attr ( ' class ' , ' text-2xl ' )
-> attr ( ' style ' , ' color: yellow; ' );
// <svg class="text-2xl" style="color: yellow;" […]>…</svg>
echo icon ( ' material-symbols:bolt ' )
-> attributes ([
' class ' => ' text-2xl ' ,
' style ' => ' color: yellow; '
]);
// <svg class="text-2xl" style="color: yellow;" […]>…</svg>
Tipp
Suchen und kopieren Sie die Symbolschlüssel beliebter Open-Source-Symbolsätze aus dem Index von Iconify.
Wichtig
Beim ersten Definieren von Symbolen wird ein Platzhalter ( �
�) angezeigt.
Stellen Sie sicher, dass Sie den scan
Befehl ausführen, um die SVGs zu laden.
vendor/bin/php-icons scan
Der scan
Befehl führt eine statische Analyse aller PHP-Dateien in Ihren konfigurierten Pfaden durch, um Symbolschlüssel ( {prefix}:{name}
) zu identifizieren und die entsprechenden Symbole herunterzuladen.
Standardmäßige Verwendung der icon
:
icon(…)
-Funktionen
echo icon ( ' ri:php-fill ' ) // identified "ri:php-fill"
@icon(…)
-Anmerkungen in Kommentaren
// @icon('ri:heart-fill') --> identified "ri:heart-fill"
# @icon('ri:home-fill') --> identified "ri:home-fill"
/*
* @icon('ri:user-fill') --> identified "ri:user-fill"
* @icon('ri:group-fill') --> identified "ri:group-fill"
*/
Ihre Konfigurationsdatei wird sowohl vom CLI-Tool php-icons
als auch von der PHPIcons-Klasse geladen. Sie sollte folgendermaßen aussehen:
<?php
declare (strict_types= 1 );
use PHPIcons Config PHPIconsConfig ;
return PHPIconsConfig:: configure ()
-> withPaths ([
__DIR__ . ' /src '
])
-> withDefaultPrefix ( '' )
-> withPlaceholder ( ' � ' );
withPaths([])
Liste der Pfade zu Ihren Quelldateien. PHP-Dateien werden analysiert und gescannt, um die von Ihnen definierten Symbole zu erkennen.
withAPIHosts([])
Ikonifizieren Sie API-Hosts, um nach dem Herunterladen von SVG-Symbolen zu fragen. Beginnt mit der Abfrage des ersten Hosts, der Rest wird als Backup verwendet.
Standardmäßig werden die öffentlichen Hosts von Iconify verwendet: ["https://api.iconify.design","https://api.simplesvg.com", "https://api.unisvg.com"]
withLocalIconSets([])
Wenn Sie benutzerdefinierte Symbole haben, können PHP-Icons diese lokal in Ihrem Dateisystem nachschlagen, anstatt die Iconify-API aufzurufen.
Wichtig
php-icons sucht in Ihren lokalen Symbolsätzen nach {name}.svg
Dateien
Nimmt ein assoziatives Array mit dem Icon-Set-Präfix als Schlüssel und seinem Pfad als Wert auf.
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 icon ( ' custom:heart ' );
echo icon ( ' custom:rocket ' );
echo icon ( ' custom:star ' );
echo icon ( ' custom:user ' );
// ICONS NOT FOUND
echo icon ( ' custom:banana ' );
echo icon ( ' custom:key ' );
withDefaultPrefix('')
Standardpräfix für Symbolsätze, das verwendet wird, wenn keines festgelegt ist.
Wenn material-symbols
als Standardpräfix festgelegt sind:
// this
echo icon ( ' bolt ' );
// same as this
echo icon ( ' material-symbols:bolt ' );
withDefaultIcon()
Standardsymbol, das verwendet wird, wenn kein Symbol gefunden wurde.
Nimmt einen Symbolschlüssel {prefix}:{name}
auf. Wenn kein Präfix festgelegt ist, wird stattdessen das Standardpräfix verwendet.
withDefaultIconPerSet([])
Standardsymbol, das verwendet wird, wenn in einem Satz kein Symbol gefunden wurde.
Nimmt ein assoziatives Array auf, wobei der Schlüssel das Präfix des Symbolsatzes und der Wert das Standardsymbol ist.
withPlaceholder('�')
Zeichenfolge, die angezeigt wird, wenn das Symbol nicht gefunden wird oder unbekannt ist.
Standardmäßig ist �
ERSATZZEICHEN).
withIdentifiers([])
Passende Funktions- oder Methodennamen zur Identifizierung von Symbolschlüsseln in Ihren Quelldateien.
Standardmäßig ist ['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
Dies wäre ohne die großartige Arbeit des Iconify-Teams und der Designer, die die vielen Open-Source-Icon-Sets verwalten, nicht möglich gewesen.
Inspiriert von Astro-Icon, Blade-Icons und Rector.
Code veröffentlicht unter der MIT-Lizenz.
Copyright (c) 2024-heute, Yassine Doghri (@yassinedoghri).