承接 sowrensen/svg-avatar-generator 相关项目开发

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

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

sowrensen/svg-avatar-generator

Composer 安装命令:

composer require sowrensen/svg-avatar-generator

包简介

Offline SVG avatar generator for Laravel

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

If you find this package useful, please consider to ⭐ it. That would be lit!

Generating SVG avatars on the fly is nothing new. There are tons of free/paid services and packages available to do that. So, why another package for same task?

Well, this one has some subtle but nifty advantages over available packages, here's a few of them:

  • Supports custom font. 🧣
  • Supports gradient background. 🦜
  • Supports random gradients based on defined presets in config. 🦚
  • Multiple shapes: rectangular, rounded-rectangular, or circular. 💎
  • Ability to customize initials and extractor. ✍🏼
  • No external api call is required, it's totally offline. 🛰️
  • Unlike some other available options, doesn't require heavy-weight image processing libraries like Intervention. 🧺
  • Doesn't have any binary dependency, so nothing needs to be installed on server. 🗃️

Requirements

Laravel PHP SVGAvatarGenerator
>=12.x >=8.2 3.x
11.x >=8.2 2.x
9.x-10.x >=8.1 1.x

Installation

Install the package via composer (Laravel 12 & 13):

composer require sowrensen/svg-avatar-generator:^3.0

Laravel 11:

composer require sowrensen/svg-avatar-generator:^2.0

For older versions:

composer require sowrensen/svg-avatar-generator:^1.0

Caution

Breaking change: Named color support, e.g. red, green in foreground and gradient_colors is dropped since version 2.0. If you're using such names in config or in code, you should change them to hexadecimal code or keep using version 1.x releases.

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="svg-avatar-generator-config"

Important

You should republish the config file after updating.

Usage

As model accessor

The usage of this package is stupidly simple. Use the svg-avatar.php config file to set your preferred decoration. Then use the Facade to generate an avatar on the fly. The recommended way to achieve that is defining an accessor in your model:

use Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\Casts\Attribute;

class User extends Model
{
    //...
    
    public function profilePhoto(): Attribute
    {
        return Attribute::get(function ($value, $attributes) {
            // If profile photo is set, return the original
            if (! is_null($attributes['profile_photo'])) {
                return $attributes['profile_photo'];
            }
            
            // Else, generate one
            return \Svg::for($attributes['name'])->toUrl();
        });
    }
}

Tip

If your accessor is different from the original attribute, you might want to put it in $appends array so that it loads automatically with your model.

Override default config

If you want you can generate an avatar totally different from your configured style. It has all helpful methods to make that possible:

use Sowren\SvgAvatarGenerator\Facades\Svg;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;

Svg::for('John Doe')
    ->asCircle() // or, asRectangle() along with optional setCornerRadius($radius) method
    ->setSize(64)
    ->setCustomFontUrl('https://api.fontshare.com/v2/css?f[]=kola@400&display=swap')
    ->setFontFamily('Kola')
    ->setFontSize(40)
    ->setFontWeight(FontWeight::SEMIBOLD)
    ->setForeground('#FFFFFF')
    ->setGradientColors( // set of 3 different gradients
      ['#4158D0', '#C850C0', '#FFCC70'], 
      ['#00DBDE', '#FC00FF'], 
      ['#FF9A8B', '#FF6A88', '#FF99AC']
    )
    ->setGradientStops(0, .5, 1)
    ->setGradientRotation(120)
    ->render();

Customize initials

You can define the second initial using studly case. For example,

Provided string Produced initial
John Doe JD
JohnDoe JD
Johndoe JO
JohndoE JE

Customize Extractor

The default initial extractor class produces results shown above. However, if you want something different, you can create your own Extractor class. To do so create a new class that implements Sowren\SvgAvatarGenerator\Extractors\Extractor interface. An ideal place to put this class would be under App\Extractors namespace in your app directory.

<?php

namespace App\Extractors;

use Sowren\SvgAvatarGenerator\Extractors\Extractor;

class CustomExtractor implements Extractor
{
    public function extract(string $text): string
    {
        // Write your custom logic and return initials
    }
}

After doing that, set the class as default extractor in config.

// ...
'extractor' => App\Extractors\CustomExtractor::class,

Sample Output

Testing

Run following command to execute test cases:

composer test

Changelog

Please see respective CHANGELOG-x.x file for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

sowrensen/svg-avatar-generator 适用场景与选型建议

sowrensen/svg-avatar-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.59k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「avatar」 「avatar-generator」 「sowrensen」 「svg-avatar-generator」 「svg-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sowrensen/svg-avatar-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sowrensen/svg-avatar-generator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-16