Laravolt/ avatar
根据任何用户的(缩写)名称显示唯一的avatar 。
预览
视频教程
安装
该包装最初是为Laravel构建的,但也可以在任何PHP项目中使用。
在此处阅读有关与PHP项目集成的更多信息。
Laravel> = 5.2:
composer require laravolt/ avatar
Laravel 5.1:
composer require laravolt/ avatar ~ 0.3
服务提供商和立面
注意:仅适用于Laravel 5.4及以下,因为由于Laravel 5.5,我们使用包装自动发现。
avatar\\ServiceProvider::class,
…
\’ avatar \’ => Laravolt\\ avatar \\Facade::class,\”>
Laravolt \\ avatar \\ServiceProvider::class, ... \' avatar \' => Laravolt \\ avatar \\Facade::class,
发布配置(可选)
php artisan vendor:publish -- provider =\"Laravolt\\ avatar \\ ServiceProvider \"
这将创建位于config/laravolt/ avatar .php中的配置文件。
管腔服务提供商
$ app -> register ( Laravolt \\ avatar \\LumenServiceProvider);
用法
输出为base64
avatar::create(\’Joko Widodo\’)->toBase64();
//use in view
//this will display initials JW as an image
<img src=\”{{ avatar ::create(\’Joko Widodo\’)->toBase64() }}\” />\”>
//this will output data-uri (base64 image data) //something like data:image/png;base64,iVBORw0KGg.... avatar :: create ( \' Joko Widodo \' )-> toBase64 (); //use in view //this will display initials JW as an image <img src=\"{{ avatar ::create(\'Joko Widodo\')->toBase64() }}\" />
另存为文件
avatar::create(\’Susilo Bambang Yudhoyono\’)->save(\’sample.png\’);
avatar ::create(\’Susilo Bambang Yudhoyono\’)->save(\’sample.jpg\’, 100); // quality = 100\”>
avatar :: create ( \' Susilo Bambang Yudhoyono \' )-> save ( \' sample.png \' ); avatar :: create ( \' Susilo Bambang Yudhoyono \' )-> save ( \' sample.jpg \' , 100 ); // quality = 100
作为GR avatar输出
avatar ();
// Output: http://*grava**tar.com/ avatar /0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee
avatar ::create(\’uyab@example.net\’)->toGr avatar ([\’d\’ => \’identicon\’, \’r\’ => \’pg\’, \’s\’ => 100]);
// Output: http://*grava**tar.com/ avatar /0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee?d=identicon&r=pg&s=100\”>
avatar :: create ( \' uyab@example.net \' )-> toGr avatar (); // Output: http://*grava**tar.com/ avatar /0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee avatar :: create ( \' uyab@example.net \' )-> toGr avatar ([ \' d \' => \' identicon \' , \' r \' => \' pg \' , \' s \' => 100 ]); // Output: http://*grava**tar.com/ avatar /0c5cbf5a8762d91d930795a6107b2ce5814a6ab26e60c7ec6b75bc81c7dfe3ee?d=identicon&r=pg&s=100
GR avatar参数参考:https://docs.gravatar.com/api/ avatar s/images/
输出为SVG
avatar :: create ( \' Susilo Bambang Yudhoyono \' )-> toSvg ();
您可以为您的SVG文本指定自定义字体家庭。
<link href="https://fonts.**g*oogleapis.com/css?family=Laravolt" rel="stylesheet">
<style>
@font-face {
font-family: Laravolt;
src: url({{ asset(\’fonts/laravolt.woff\’)) }});
}
</style>
</head>\”>
< head > <!--Prepare custom font family, using Google Fonts--> < link href =\" https://fonts.*g*ooglea*pis.com/css?family=Laravolt \" rel =\" stylesheet \" > <!--OR--> <!--Setup your own style--> < style > @font-face { font-family : Laravolt; src : url ({{ asset( \'fonts/laravolt.woff\' )) }}); } </ style > </ head >
avatar :: create ( \' Susilo Bambang Yudhoyono \' )-> setFontFamily ( \' Laravolt \' )-> toSvg ();
您可以使SVG响应迅速。这不包括高度和宽度属性。
avatar :: create ( \' Susilo Bambang Yudhoyono \' )-> setResponsive ()-> toSvg ();
获取基础干预图像对象
avatar :: create ( \' Abdul Somad \' )-> getImageObject ();
该方法将返回干预图像对象的实例,因此您可以将其用于进一步的目的。
非ASCII角色
默认情况下,此软件包将尝试输出任何缩写字母。如果提供的名称包含任何非ASCII字符(例如,例如,例如,请参见Config)。字体支持提供的字符,它将成功显示,否则不会。
另外,我们可以将所有非ASCII转换为最接近的ASCII对应物。如果找不到最接近的CoutnerParts,则将删除这些字符。感谢Stringy提供了此类有用的功能。我们需要的只是在config/ avatar .php中更改一行:
\' ascii \' => true,
配置
avatar use Intervention Image library to process image.
| Meanwhile, Intervention Image supports \”GD Library\” and \”Imagick\” to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP\’s \”Imagick\” implementation is used.
|
| Supported: \”gd\”, \”imagick\”
|
*/
\’driver\’ => \’gd\’,
// Initial generator class
\’generator\’ => \\Laravolt\\ avatar \\Generator\\DefaultGenerator::class,
// Whether all characters supplied must be replaced with their closest ASCII counterparts
\’ascii\’ => false,
// Image shape: circle or square
\’shape\’ => \’circle\’,
// Image width, in pixel
\’width\’ => 100,
// Image height, in pixel
\’height\’ => 100,
// Number of characters used as initials. If name consists of single word, the first N character will be used
\’chars\’ => 2,
// font size
\’fontSize\’ => 48,
// convert initial letter in uppercase
\’uppercase\’ => false,
// Right to Left (RTL)
\’rtl\’ => false,
// Fonts used to render text.
// If contains more than one fonts, randomly selected based on name supplied
\’fonts\’ => [__DIR__.\’/../fonts/OpenSans-Bold.ttf\’, __DIR__.\’/../fonts/rockwell.ttf\’],
// List of foreground colors to be used, randomly selected based on name supplied
\’foregrounds\’ => [
\’#FFFFFF\’,
],
// List of background colors to be used, randomly selected based on name supplied
\’backgrounds\’ => [
\’#f44336\’,
\’#E91E63\’,
\’#9C27B0\’,
\’#673AB7\’,
\’#3F51B5\’,
\’#2196F3\’,
\’#03A9F4\’,
\’#00BCD4\’,
\’#009688\’,
\’#4CAF50\’,
\’#8BC34A\’,
\’#CDDC39\’,
\’#FFC107\’,
\’#FF9800\’,
\’#FF5722\’,
],
\’border\’ => [
\’size\’ => 1,
// border color, available value are:
// \’foreground\’ (same as foreground color)
// \’background\’ (same as background color)
// or any valid hex (\’#aabbcc\’)
\’color\’ => \’background\’,
// border radius, only works for SVG
\’radius\’ => 0,
],
// List of theme name to be used when rendering avatar
// Possible values are:
// 1. Theme name as string: \’colorful\’
// 2. Or array of string name: [\’grayscale-light\’, \’grayscale-dark\’]
// 3. Or wildcard \”*\” to use all defined themes
\’theme\’ => [\’*\’],
// Predefined themes
// Available theme attributes are:
// shape, chars, backgrounds, foregrounds, fonts, fontSize, width, height, ascii, uppercase, and border.
\’themes\’ => [
\’grayscale-light\’ => [
\’backgrounds\’ => [\’#edf2f7\’, \’#e2e8f0\’, \’#cbd5e0\’],
\’foregrounds\’ => [\’#a0aec0\’],
],
\’grayscale-dark\’ => [
\’backgrounds\’ => [\’#2d3748\’, \’#4a5568\’, \’#718096\’],
\’foregrounds\’ => [\’#e2e8f0\’],
],
\’colorful\’ => [
\’backgrounds\’ => [
\’#f44336\’,
\’#E91E63\’,
\’#9C27B0\’,
\’#673AB7\’,
\’#3F51B5\’,
\’#2196F3\’,
\’#03A9F4\’,
\’#00BCD4\’,
\’#009688\’,
\’#4CAF50\’,
\’#8BC34A\’,
\’#CDDC39\’,
\’#FFC107\’,
\’#FF9800\’,
\’#FF5722\’,
],
\’foregrounds\’ => [\’#FFFFFF\’],
],
]
];\”>
<?php /* * Set specific configuration variables here */ return [ /* |-------------------------------------------------------------------------- | Image Driver |-------------------------------------------------------------------------- | avatar use Intervention Image library to process image. | Meanwhile, Intervention Image supports \"GD Library\" and \"Imagick\" to process images | internally. You may choose one of them according to your PHP | configuration. By default PHP\'s \"Imagick\" implementation is used. | | Supported: \"gd\", \"imagick\" | */ \' driver \' => \' gd \' , // Initial generator class \' generator \' => \\ Laravolt \\ avatar \\ Generator \\DefaultGenerator::class, // Whether all characters supplied must be replaced with their closest ASCII counterparts \' ascii \' => false , // Image shape: circle or square \' shape \' => \' circle \' , // Image width, in pixel \' width \' => 100 , // Image height, in pixel \' height \' => 100 , // Number of characters used as initials. If name consists of single word, the first N character will be used \' chars \' => 2 , // font size \' fontSize \' => 48 , // convert initial letter in uppercase \' uppercase \' => false , // Right to Left (RTL) \' rtl \' => false , // Fonts used to render text. // If contains more than one fonts, randomly selected based on name supplied \' fonts \' => [ __DIR__ . \' /../fonts/OpenSans-Bold.ttf \' , __DIR__ . \' /../fonts/rockwell.ttf \' ], // List of foreground colors to be used, randomly selected based on name supplied \' foregrounds \' => [ \' #FFFFFF \' , ], // List of background colors to be used, randomly selected based on name supplied \' backgrounds \' => [ \' #f44336 \' , \' #E91E63 \' , \' #9C27B0 \' , \' #673AB7 \' , \' #3F51B5 \' , \' #2196F3 \' , \' #03A9F4 \' , \' #00BCD4 \' , \' #009688 \' , \' #4CAF50 \' , \' #8BC34A \' , \' #CDDC39 \' , \' #FFC107 \' , \' #FF9800 \' , \' #FF5722 \' , ], \' border \' => [ \' size \' => 1 , // border color, available value are: // \'foreground\' (same as foreground color) // \'background\' (same as background color) // or any valid hex (\'#aabbcc\') \' color \' => \' background \' , // border radius, only works for SVG \' radius \' => 0 , ], // List of theme name to be used when rendering avatar // Possible values are: // 1. Theme name as string: \'colorful\' // 2. Or array of string name: [\'grayscale-light\', \'grayscale-dark\'] // 3. Or wildcard \"*\" to use all defined themes \' theme \' => [ \' * \' ], // Predefined themes // Available theme attributes are: // shape, chars, backgrounds, foregrounds, fonts, fontSize, width, height, ascii, uppercase, and border. \' themes \' => [ \' grayscale-light \' => [ \' backgrounds \' => [ \' #edf2f7 \' , \' #e2e8f0 \' , \' #cbd5e0 \' ], \' foregrounds \' => [ \' #a0aec0 \' ], ], \' grayscale-dark \' => [ \' backgrounds \' => [ \' #2d3748 \' , \' #4a5568 \' , \' #718096 \' ], \' foregrounds \' => [ \' #e2e8f0 \' ], ], \' colorful \' => [ \' backgrounds \' => [ \' #f44336 \' , \' #E91E63 \' , \' #9C27B0 \' , \' #673AB7 \' , \' #3F51B5 \' , \' #2196F3 \' , \' #03A9F4 \' , \' #00BCD4 \' , \' #009688 \' , \' #4CAF50 \' , \' #8BC34A \' , \' #CDDC39 \' , \' #FFC107 \' , \' #FF9800 \' , \' #FF5722 \' , ], \' foregrounds \' => [ \' #FFFFFF \' ], ], ] ];
运行时覆盖配置
我们可以通过使用以下功能在运行时覆盖配置:
avatar::create(\’Soekarno\’)->setDimension(100);//width = height = 100 pixel
avatar ::create(\’Soekarno\’)->setDimension(100, 200); // width = 100, height = 200
avatar ::create(\’Soekarno\’)->setBackground(\’#001122\’);
avatar ::create(\’Soekarno\’)->setForeground(\’#999999\’);
avatar ::create(\’Soekarno\’)->setFontSize(72);
avatar ::create(\’Soekarno\’)->setFont(\’/path/to/font.ttf\’);
avatar ::create(\’Soekarno\’)->setBorder(1, \’#aabbcc\’); // size = 1, color = #aabbcc
avatar ::create(\’Soekarno\’)->setBorder(1, \’#aabbcc\’, 10); // size = 1, color = #aabbcc, border radius = 10 (only for SVG)
avatar ::create(\’Soekarno\’)->setShape(\’square\’);
// Available since 3.0.0
avatar ::create(\’Soekarno\’)->setTheme(\’colorful\’); // set exact theme
avatar ::create(\’Soekarno\’)->setTheme([\’grayscale-light\’, \’grayscale-dark\’]); // theme will be randomized from these two options
// chaining
avatar ::create(\’Habibie\’)->setDimension(50)->setFontSize(18)->toBase64();\”>
avatar :: create ( \' Soekarno \' )-> setDimension ( 100 ); //width = height = 100 pixel avatar :: create ( \' Soekarno \' )-> setDimension ( 100 , 200 ); // width = 100, height = 200 avatar :: create ( \' Soekarno \' )-> setBackground ( \' #001122 \' ); avatar :: create ( \' Soekarno \' )-> setForeground ( \' #999999 \' ); avatar :: create ( \' Soekarno \' )-> setFontSize ( 72 ); avatar :: create ( \' Soekarno \' )-> setFont ( \' /path/to/font.ttf \' ); avatar :: create ( \' Soekarno \' )-> setBorder ( 1 , \' #aabbcc \' ); // size = 1, color = #aabbcc avatar :: create ( \' Soekarno \' )-> setBorder ( 1 , \' #aabbcc \' , 10 ); // size = 1, color = #aabbcc, border radius = 10 (only for SVG) avatar :: create ( \' Soekarno \' )-> setShape ( \' square \' ); // Available since 3.0.0 avatar :: create ( \' Soekarno \' )-> setTheme ( \' colorful \' ); // set exact theme avatar :: create ( \' Soekarno \' )-> setTheme ([ \' grayscale-light \' , \' grayscale-dark \' ]); // theme will be randomized from these two options // chaining avatar :: create ( \' Habibie \' )-> setDimension ( 50 )-> setFontSize ( 18 )-> toBase64 ();
与其他PHP项目集成
avatar class
use Laravolt\\ avatar \\ avatar ;
// create your first avatar
$ avatar = new avatar ($config);
$ avatar ->create(\’John Doe\’)->toBase64();
$ avatar ->create(\’John Doe\’)->save(\’path/to/file.png\’, $quality = 90);\”>
// include composer autoload require \' vendor/autoload.php \' ; // import the avatar class use Laravolt \\ avatar \\ avatar ; // create your first avatar $ avatar = new avatar ( $ config ); $ avatar -> create ( \' John Doe \' )-> toBase64 (); $ avatar -> create ( \' John Doe \' )-> save ( \' path/to/file.png \' , $ quality = 90 );
$config只是一个普通数组,其格式与上述相同(请参阅配置)。
