chelout/avatar

将名称、电子邮件和其他任何字符串转换为具有阴影的基于首字母的美丽头像。

v0.3 2019-07-05 12:23 UTC

This package is auto-updated.

Last update: 2024-08-29 04:20:09 UTC


README

SensioLabsInsight Travis Coverage Status

Preview

根据用户的(首字母)名称显示任何用户的独特头像。

预览

Preview

安装

此包最初是为 Laravel 构建的,但也可以在任何 PHP 项目中使用。

在此处了解更多关于与 PHP 项目集成的信息。

Laravel 5.2/5.3/5.4/5.5

$ composer require laravolt/avatar

Laravel 5.1

composer require laravolt/avatar ~0.3

服务提供者 & Facade

注意:仅适用于 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 中创建配置文件。

用法

以 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

以 SVG 输出

Avatar::create('Susilo Bambang Yudhoyono')->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 "GD Library" 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 to uppercase
    'uppercase' => false,

    // Fonts used to render text.
    // If contains more than one fonts, randomly selected based on name supplied
    'fonts'    => ['path/to/OpenSans-Bold.ttf', 'path/to/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' => 'foreground'
    ]
];

运行时覆盖配置

我们可以使用以下函数在运行时覆盖配置

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')->setShape('square');

// 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 是一个格式与上述解释相同的普通数组(请参阅 配置)。