承接 martian/laravatar 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

martian/laravatar

Composer 安装命令:

composer require martian/laravatar

包简介

🚀 A lightweight and easy-to-use Laravel package that add avatars to your models

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

🚀 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.php file.
'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 我们能提供哪些服务?
定制开发 / 二次开发

基于 martian/laravatar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 160
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-29