baruch4413/identicon

一个用于生成identicons的PHP库。

1.1.2 2018-03-03 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:33:54 UTC


README

Code Climate Test Coverage

这是一个基于给定字符串生成identicons的PHP库。目前只支持SVG格式,因为它提供了效率、质量、文件大小和易用性的最佳组合。

要与symfony应用程序集成,可以使用BitverseIdenticonBundle

安装

使用composer安装

$ composer require baruch4413/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