martian/laravatar
Composer 安装命令:
composer require martian/laravatar
包简介
🚀 A lightweight and easy-to-use Laravel package that add avatars to your models
关键字:
README 文档
README
🚀 A lightweight and easy-to-use Laravel package designed to simplify avatar generation for your Eloquent models. It provides a flexible and extensible solution for generating avatars using Gravatar, DiceBear, UI Avatars or Boring Avatar
Supported Avatar Drivers
| Driver | Description | Supported | Link |
|---|---|---|---|
| Gravatar | Gravatar is a service for providing globally unique avatars. | Yes ✅️ | Gravatar |
| DiceBear | DiceBear is an avatar library for designers and developers. | Yes ✅️ | DiceBear |
| UI Avatars | UI Avatars is an avatar library for designers and developers. | Yes ✅️ | UI Avatars |
| Boring Avatar | Boring avatars is a tiny JavaScript React library that generates custom, SVG-based, round avatars from any username and color palette. | Yes ✅️ | Boring Avatar |
Note
You can also add your own custom driver by implementing the AmplifiedHQ\Laravatar\Contracts\AvatarInterface; interface and extending the AmplifiedHQ\Laravatar\Abstracts\BaseAvatar class.
Installation
Note: This package requires PHP 7.4 or higher. You can install the package via composer:
composer require martian/laravatar
Register Service Provider
Add the service provider to the providers array in config/app.php:
AmplifiedHQ\Laravatar\Providers\LaravatarServiceProvider::class,
Publish Configuration File
Publish the configuration file using the following command:
php artisan vendor:publish --provider="AmplifiedHQ\Laravatar\Providers\LaravatarServiceProvider" --tag="config"
Configuration
You can configure the package by editing the config/laravatar.php file.
- In the configuration file you can specify the default avatar driver to use across your application.
'default' => env('LARAVATAR_DRIVER', 'gravatar'),
- If you want to change the default options for a driver, you can do so by editing the
config/laravatar.phpfile.
'drivers' => [ ... 'gravatar' => [ 'class' => \AmplifiedHQ\Laravatar\Drivers\Gravatar::class, 'options' => [ 'size' => 96, ], ], ... ],
Usage
In order to use the package in your model to generate an avatar on the fly, you need to add the AmplifiedHQ\Laravatar\Traits\HasAvatar trait to your model.
Using the HasAvatar trait
namespace App\Models; use AmplifiedHQ\Laravatar\HasAvatar; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, HasAvatar; /** @var Avatar Column */ protected $avatarColumn = 'email'; /** @var Avatar Storage Column */ protected $avatarStorageColumn = 'avatar'; ... }
Important
If you are using the gravatar driver, you need to use the email column as the avatar column. If you are using the dicebear or ui-avatars or boringavatar driver, you can use any column as the avatar column, provided that the column is a string column. (e.g. name, email, username etc.)
Warning
The HasAvatar trait requires you to define the $avatarColumn and $avatarStorageColumn properties in your model. The $avatarColumn property is the column that will be used to generate the avatar. The $avatarStorageColumn property is the column that will be used to store the avatar.
Using the Driver Methods
You can also use each driver method directly on your application either on on your controller, model or view.
Gravatar
use AmplifiedHQ\Laravatar\Drivers\Gravatar; use App\Http\Controllers\Controller; class UserController extends Controller { public function generateAvatar() { $gravatar = new Gravatar('jane@example.com'); $gravatar->setSize(100); $gravatar->getUrl(); // https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf?size=100 } }
DiceBear
use AmplifiedHQ\Laravatar\Drivers\DiceBear; use App\Http\Controllers\Controller; class UserController extends Controller { public function generateAvatar() { $dicebear = new DiceBear('Jane Doe'); $dicebear->setStyle('lorelei'); $dicebear->setSize(100); $dicebear->setFormat('svg'); $dicebear->getUrl(); // https://api.dicebear.com/7.x/lorelei/svg?seed=Jane%20Doe&size=100 } }
Boring Avatar
use AmplifiedHQ\Laravatar\Drivers\BoringAvatar; use App\Http\Controllers\Controller; class UserController extends Controller { public function generateAvatar() { $boringAvatar = new BoringAvatar('Jane Doe'); $boringAvatar->setSize(100); $boringAvatar->setFormat('svg'); $boringAvatar->setVariant('marble'); $boringAvatar->setSquare(true); $boringAvatar->getUrl(); // https://boring-avatars-api.vercel.app/api/avatar?size=100&variant=marble&name=Jane%20Doe&sqaure=1 } }
UI Avatars
use AmplifiedHQ\Laravatar\Drivers\UiAvatars; use App\Http\Controllers\Controller; class UserController extends Controller { public function generateAvatar() { $uiAvatars = new UiAvatars('Jane Doe'); $uiAvatars->setSize(100); $uiAvatars->setFormat('svg'); $uiAvatars->setBackgroundColor('000000'); // Hexadecimal $uiAvatars->setForegroundColor('ffffff'); // Hexadecimal $uiAvatars->setRounded(true); $uiAvatars->getUrl(); // https://ui-avatars.com/api/?name=Jane%20Doe&size=100&background=000000&color=ffffff&rounded=true } }
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email hendurhance.dev@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
martian/laravatar 适用场景与选型建议
martian/laravatar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「gravatar」 「php」 「laravel」 「avatars」 「avatar」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 martian/laravatar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 martian/laravatar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 martian/laravatar 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library providing easy gravatar integration/generation (Laravel supported).
Library to validate and parse social media profile URLs
Gravatar creator for Nette Framework
Gravel is a PHP library which provides easy access to get and display Gravatars.
Get Gravatar image URL and put by default an image with username initials, You can test this library here https://gravatar-url.herokuapp.com/examples/LibraryUse.php
Alfabank REST API integration
统计信息
- 总下载量: 160
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-29