bitverse / identicon
用于生成 identicons 的 PHP 库。
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is not auto-updated.
Last update: 2024-09-14 18:18:05 UTC
README
这是一个基于给定字符串生成 identicons 的 PHP 库。目前仅支持 SVG 格式,因为它在效率、质量、文件大小和易用性方面提供了最佳组合。
对于与 symfony 应用的集成,有 BitverseIdenticonBundle
安装
使用 composer 安装
$ composer require bitverse/identicon
用法
以下代码将根据字符串 "hello world" 创建一个 rings-identicon,并将其保存到 helloworld.svg
。
<?php use Bitverse\Identicon\Identicon; use Bitverse\Identicon\Color\Color; use Bitverse\Identicon\Generator\RingsGenerator; use Bitverse\Identicon\Preprocessor\MD5Preprocessor; $generator = new RingsGenerator(); $generator->setBackgroundColor(Color::parseHex('#EEEEEE')); $identicon = new Identicon(new MD5Preprocessor(), $generator); $icon = $identicon->getIcon('hello world'); file_put_contents('helloworld.svg', $icon);
Identicon 对象
Identicon 是负责根据给定字符串提供 identicons 的主要服务。为了做到这一点,需要在构造函数中传递一个预处理程序和一个生成器。
然后可以调用 getIcon()
函数来创建一个新图标。
预处理程序
预处理程序是一个实现了 Bitverse\Identicon\Preprocessor\PreprocessorInterface
的对象。
预处理程序的作用是在将字符串传递给生成器的 generate()
方法之前对其进行任何操作。这包括哈希处理。
目前,该库仅包括 MD5Preprocessor
,它使用 MD5 算法对给定字符串进行哈希处理。
生成器
生成器负责根据预处理程序生成的哈希值实际生成图标。
任何实现了 Bitverse\Identicon\Generator\GeneratorInterface
的对象都可以作为生成器。
目前,该库默认提供了两个生成器:
-
RingsGenerator
生成由中心点和三个不同长度和旋转的环组成的 identicons。 -
PixelsGenerator
生成 5x5 像素的图标,类似于在 github 上使用的图标。
示例
以下是 'helloworld' 的示例
MD5Preprocessor
+RingsGenerator
MD5Preprocessor
+PixelsGenerator