usarise / identicon
用于生成 identicons 的 PHP 库。
0.8.2
2024-09-07 14:02 UTC
Requires
- php: ^8.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.43.0
- friendsofphp/php-cs-fixer: ^3.64.0
- phpstan/phpstan: ^1.12.2
- phpunit/phpunit: ^10.5.32
- rector/rector: ^1.2.4
Suggests
- ext-gd: to use the GD driver
- ext-imagick: to use the Imagick driver
README
基于并受以下启发
- github_avatars_generator (矩阵)
- identicon (颜色)
安装
composer require usarise/identicon
浏览器使用
<?php declare(strict_types=1); require_once __DIR__ . '/vendor/autoload.php'; use Usarise\Identicon\Identicon; use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas; $identicon = new Identicon( new SvgCanvas(), 420, ); $response = $identicon->generate('test'); header("Content-type: {$response->mimeType}"); echo (string) $response;
写入文件使用
<?php declare(strict_types=1); require_once __DIR__ . '/vendor/autoload.php'; use Usarise\Identicon\Identicon; use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas; $identicon = new Identicon( new SvgCanvas(), 420, ); $response = $identicon->generate('test'); $response->save("test.{$response->format}");
数据 URL 使用
<?php declare(strict_types=1); require_once __DIR__ . '/vendor/autoload.php'; use Usarise\Identicon\Identicon; use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas; $identicon = new Identicon( new SvgCanvas(), 420, ); $response = $identicon->generate('test'); $data = sprintf( 'data:%s;base64,%s', $response->mimeType, base64_encode( (string) $response, ), ); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Identicon Data URL example</title> </head> <body> <img src="<?php echo $data; ?>" /> </body> </html>
高级使用
构造
use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas; use Usarise\Identicon\{Identicon, Resolution}; $identicon = new Identicon( image: new SvgCanvas(), // implementation Usarise\Identicon\Image\CanvasInterface size: 420, // 420x420 pixels resolution: Resolution::Medium, // Resolution 10x10 (Default) );
图像画布
从箱子中实现 Usarise\Identicon\Image\CanvasInterface
use Usarise\Identicon\Image\Gd\Canvas as GdCanvas; use Usarise\Identicon\Image\Imagick\Canvas as ImagickCanvas; use Usarise\Identicon\Image\Svg\Canvas as SvgCanvas;
GD 扩展 (GD 库)
new GdCanvas()
Imagick 扩展 (ImageMagick)
new ImagickCanvas()
SVG
new SvgCanvas()
大小
输出图像高度和宽度
必须是分辨率的正整数倍
示例:对于 Resolution::Medium
分辨率是 120,对于 Resolution::Large
分辨率是 126
分辨率
图案 identicon 的像素分辨率
use Usarise\Identicon\Resolution;
6x6 (微型)
Resolution::Tiny
8x8 (小型)
Resolution::Small
10x10 (中型)
Resolution::Medium
12x12 (大型)
Resolution::Large
14x14 (巨型)
Resolution::Huge
生成
字符串
用户名、id、电子邮件、ip 等
$response = $identicon->generate(string: 'test')
背景
CSS 6 位或 3 位十六进制颜色
$response = $identicon->generate(string: 'test', background: '#f2f1f2')
前景
CSS 6 位或 3 位十六进制颜色
$response = $identicon->generate(string: 'test', foreground: '#84c7b5')
背景和前景
CSS 6 位或 3 位十六进制颜色
$response = $identicon->generate(string: 'test', background: '#f2f1f2', foreground: '#84c7b5')
响应
格式
png
,svg
,其他自定义
$response->format // svg
MIME 类型
image/png
,image/svg+xml
,其他自定义
$response->mimeType // image/svg+xml
输出字符串
压缩图像
$response->output
替代 $response->output
:将响应对象转换为字符串
压缩图像
(string) $response
未压缩图像
object
,string
,null
$response->image // object
保存文件
仅允许 $response->format
中的文件扩展名
$response->save(path: __DIR__ . '/test.svg')