allenjb/initial-avatar-generator
Composer 安装命令:
composer require allenjb/initial-avatar-generator
包简介
A package to generate avatars with initials for PHP
README 文档
README
Fork of lasserafn/php-initial-avatar-generator updated to use Imagine, resolving issues with PHP 8.1
This library should be pretty much a drop-in replacement for most users with the following notable changes:
- This library uses strictly defined types
- This library returns Imagine objects instead for (non-SVG) generated avatars
- Font numbers in place of files were removed
- AutoFont functionality has been removed - you can reimplement this yourself using the lasserafn/php-string-script-language package
- Only the default Open Sans Regular font is shipped with the package
- FontAwesome files are no longer shipped with the library
Installation
You just require using composer and you're good to go!
composer require allenjb/initial-avatar-generator
Usage
As with installation, usage is quite simple. Generating a image is done by running:
$avatar = new AllenJB\InitialAvatarGenerator\InitialAvatar(); $image = $avatar->name('Lasse Rafn')->generate();
Thats it! The method will return a instance of an ImageInterface from Imagine so you can save or output the image:
return $image->show('png');
If only one word is passed, the first 2 letters will be used. If more than 2 words are passed, the initials of the first and last words will be used.
SVG generation
$avatar = new AllenJB\InitialAvatarGenerator\InitialAvatar(); echo $avatar->name('Lasse Rafn')->generateSvg()->toXMLString(); // returns SVG XML string
Supported methods and parameters
Name (initials) - default: JD
$image = $avatar->name('Albert Magnum')->generate();
Width - default: 48
// will be 96 pixels wide. $image = $avatar->width(96)->generate();
Height - default: 48
// will be 96 pixels tall. $image = $avatar->height(96)->generate();
Size - default: 48 (proxy for $avatar->width(X)->height(X))
// will be 96x96 pixels. $image = $avatar->size(96)->generate();
Background color - default: #f0e9e9
// will be red $image = $avatar->background('#ff0000')->generate();
Font color - default: #8b5d5d
// will be red $image = $avatar->color('#ff0000')->generate();
Font file - default: /fonts/OpenSans-Regular.ttf
// will be Semibold $image = $avatar->font('/path/to/fonts/OpenSans-Semibold.ttf')->generate();
Font name (for SVGs) - default: Open Sans, sans-serif
$image = $avatar->fontName('Arial, Helvetica, sans-serif')->generate();
Length - default: 2
$image = $avatar->name('John Doe Johnson')->length(3)->generate(); // 3 letters = JDJ
Switching driver - default: gd
$image = $avatar->gd()->generate(); // Uses GD driver $image = $avatar->imagick()->generate(); // Uses Imagick driver
Rounded - default: false
$image = $avatar->rounded()->generate();
Smooth - default: false
Makes rounding smoother with a resizing hack. Could be slower.
$image = $avatar->rounded()->smooth()->generate();
Alternatively consider using CSS instead.
Font Size - default: 0.5
$image = $avatar->fontSize(0.25)->generate(); // Font will be 25% of image size.
If the Image size is 50px and fontSize is 0.5, the font size will be 25px.
Chaining it all together
return $avatar->name('Lasse Rafn') ->length(2) ->fontSize(0.5) ->size(96) // 48 * 2 ->background('#8BC34A') ->color('#fff') ->generate() ->save('png');
Now, using that in a image (sized 48x48 pixels for retina):
<img src="url-for-avatar-generation" width="48" height="48" style="border-radius: 100%" />
Will yield:
Rounded for appearance; the actual avatar is a filled square
Icon Font Support
First, you need to "find" the respective unicode for the glyph you want to insert. For example, using FontAwesome, to display a typical "user" icon use unicode: f007. You can usually find the unicode character (code) on the icon font browser.
An example for rendering a red avatar with a white "user" glyph would look like this:
// note that this code // 1) uses glyph() instead of name // 2) changes the font to FontAwesome! return $avatar->glyph('f007') ->font('/path/to/fonts/FontAwesome5Free-Regular-400.otf') ->color('#fff') ->background('#ff0000') ->generate() ->save('png');
allenjb/initial-avatar-generator 适用场景与选型建议
allenjb/initial-avatar-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 190 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「avatar」 「svg」 「Initials」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 allenjb/initial-avatar-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 allenjb/initial-avatar-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 allenjb/initial-avatar-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generate an Adorable Avatar for Laravel
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
PHP class to add Font Awesome 5+'s SVG icons inline without Javascript.
Utility functions to inline SVGs in PHP Projects
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Change the default avatar url provider with one for inline SVGs
统计信息
- 总下载量: 190
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-03