identicon

package module
v2.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

identicon

Go codecov PkgGoDev Go version License

根据用户的 IP 、邮箱名等任意数据为用户产生漂亮的随机头像。

提供了两种风格的头像数据,其中 Style2 风格更加的像素风,且性能也更佳。

style1

v1.1 v1.2 v1.3 v1.4 v1.5

style2

v2.1 v2.2 v2.3 v2.4 v2.5

// 根据用户访问的IP,为其生成一张头像
img, _ := identicon.Make(Style1, 128, color.NRGBA{},color.NRGBA{}, []byte("192.168.1.1"))
fi, _ := os.Create("/tmp/u1.png")
png.Encode(fi, img)
fi.Close()

// 或者
ii, _ := identicon.New(Style2, 128, color.NRGBA{}, color.NRGBA{}, color.NRGBA{}, color.NRGBA{})
img := ii.Make([]byte("192.168.1.1"))
img = ii.Make([]byte("192.168.1.2"))

安装

go get github.com/issue9/identicon/v2

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package identicon 一个基于 hash 值生成随机图像的包

identicon 并没有统一的标准,一般用于在用户注册时, 取用户的邮箱或是访问 IP 等数据(也可以是其它任何数据), 进行 hash 运算,之后根据 hash 数据,产生一张图像, 这样即可以为用户产生一张独特的头像,又不会泄漏用户的隐藏。

提供了两种风格的头像:Style1 和 Style2。

style1

在 style1 中,把图像分成以下九个部分:

-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------

其中 1、3、9、7 为不同角度(依次增加 90 度)的同一张图片, 2、6、8、4 也是如此,这样可以保持图像是对称的,比较美观。 5 则单独使用一张图片。

style2

将用户内容计算出 32 位的 hash 值,以 4 bit 为一行, 值为 1 表示有前景色,为 0 表示没有背景色,同时镜像到右边。

// 根据用户访问的 IP ,为其生成一张头像
img := identicon.Make(Style2, 128, color.NRGBA{},color.NRGBA{}, []byte("192.168.1.1"))
fi, _ := os.Create("/tmp/u1.png")
png.Encode(fi, img)
fi.Close()

// 或者
ii := identicon.New(Style1, 128, color.NRGBA{}, color.NRGBA{}, color.NRGBA{})
img := ii.Make([]byte("192.168.1.1"))
img = ii.Make([]byte("192.168.1.2"))

NOTE: go test 会在当前目录的 testdata 文件夹下产生大量的随机图片。 要运行测试,必须保证该文件夹是存在的,且有相应的写入权限。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(style Style, size int, back, fore color.Color, data []byte) image.Image

Make 根据 data 数据产生一张唯一性的头像图片

size 头像的大小。 back, fore 头像的背景和前景色。

Types

type Identicon

type Identicon struct {
	// contains filtered or unexported fields
}

Identicon 用于产生统一尺寸的头像

可以根据用户提供的数据,经过一定的算法,自动产生相应的图案和颜色。

func New

func New(style Style, size int, back color.Color, fore ...color.Color) *Identicon

New 声明一个 Identicon 实例

style 图片风格; size 头像的大小,应该将 size 的值保持在能被 3 整除的偶数,图片才会平整; back 前景色; fore 所有可能的前景色,会为每个图像随机挑选一个作为其前景色。

func S1

func S1(size int) *Identicon

S1 采用 style1 风格的头像

背景为透明,前景由 image/color/palette.WebSafe 指定;

func S2

func S2(size int) *Identicon

S2 采用 style2 风格的头像

背景为透明,前景由 image/color/palette.WebSafe 指定;

func (*Identicon) Make

func (i *Identicon) Make(data []byte) image.Image

Make 根据 data 数据随机图片

func (*Identicon) Rand

func (i *Identicon) Rand(r *rand.Rand) image.Image

Rand 随机生成图案

type Style

type Style int8
const (
	Style1 Style = iota + 1 // 旧版本风格
	Style2                  // Style2 风格,性能略高于 Style1
)

Directories

Path Synopsis
internal
style1
Package style1 风格 1 的头像
Package style1 风格 1 的头像
style2
Package style2 风格 2 的头像
Package style2 风格 2 的头像

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL