laravolt / avatar
将名称、电子邮件和其他任何字符串转换为基于首字母的头像或Gravatar。
6.0.0
2024-09-20 17:07 UTC
Requires
- php: >=8.1
- illuminate/cache: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- intervention/image: ^3.4
Requires (Dev)
- mockery/mockery: ~1.3
- pestphp/pest: ^2.34
- pestphp/pest-plugin-type-coverage: ^2.8
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-latest
Suggests
- ext-gd: Needed to support image manipulation
- ext-imagick: Needed to support image manipulation, better than GD
- dev-master / 6.0.x-dev
- 6.0.0
- 5.x-dev
- 5.1.0
- 5.0.0
- 4.x-dev
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.x-dev
- 1.0.0
- 0.x-dev
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-shift-115119
- dev-analysis-PG2VNK
- dev-analysis-QMwMxw
- dev-analysis-QMr1mP
- dev-theming-2
This package is auto-updated.
Last update: 2024-09-20 17:13:39 UTC
README
根据用户(首字母)名称显示独特的头像。
预览
🎞️ 视频教程
安装
此包最初为Laravel构建,但也可以在任何PHP项目中使用。
Laravel >= 5.2
composer require laravolt/avatar
Laravel 5.1
composer require laravolt/avatar ~0.3
服务提供者 & 面板
注意:仅适用于Laravel 5.4及以下版本,因为自Laravel 5.5以来,我们使用包自动发现。
Laravolt\Avatar\ServiceProvider::class, ... 'Avatar' => Laravolt\Avatar\Facade::class,
发布配置(可选)
php artisan vendor:publish --provider="Laravolt\Avatar\ServiceProvider"
这将创建位于 config/laravolt/avatar.php
的配置文件。
Lumen服务提供者
$app->register(Laravolt\Avatar\LumenServiceProvider);
用法
以base64输出
//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
以Gravatar输出
Avatar::create('uyab@example.net')->toGravatar(); // Output: http://gravatar.com/avatar/0dcae7d6d76f9a3b14588e9671c45879 Avatar::create('uyab@example.net')->toGravatar(['d' => 'identicon', 'r' => 'pg', 's' => 100]); // Output: http://gravatar.com/avatar/0dcae7d6d76f9a3b14588e9671c45879?d=identicon&r=pg&s=100
Gravatar参数参考:https://en.gravatar.com/site/implement/images/
以SVG输出
Avatar::create('Susilo Bambang Yudhoyono')->toSvg();
您可以为SVG文本指定自定义字体族。
<head> <!--Prepare custom font family, using Google Fonts--> <link href="https://fonts.googleapis.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();
获取底层Intervention图像对象
Avatar::create('Abdul Somad')->getImageObject();
该方法将返回一个Intervention图像对象的实例,因此您可以将其用于其他目的。
非ASCII字符
默认情况下,此包将尝试按原样输出任何首字母。如果提供的名称包含任何非ASCII字符(例如,ā,Ě,ǽ),则结果将取决于所使用的字体(请参阅配置)。如果字体支持提供的字符,则可以成功显示,否则将无法显示。
或者,我们可以将所有非ASCII字符转换为它们最近的ASCII对应物。如果没有找到最近的对应物,则删除这些字符。感谢Stringy提供这样有用的功能。我们需要做的只是更改 config/avatar.php
中的一行。
'ascii' => true,
配置
<?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();
与其他PHP项目集成
// 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
仅是一个与上述格式相同的普通数组(请参阅配置)。