@aboveyou00/identicon 中文文档教程

发布于 7年前 浏览 17 项目主页 更新于 3年前

node-identicon-github

Dependency Status

https://github.com/sehrgut/node-retricon 的作者已经很长时间没有维护模块了(从 2013 年开始)。 一些依赖已经更新,我们需要使用它。

创建类似于 GithubGravatar(复古) 头像

头像 头像 头像 头像 头像 头像 头骨 frog

Example

var identicon = require('identicon-github');
var fmt = require('util').format;

console.log(fmt("<img alt='kibo' src='%s' />", identicon('kibo', { pixelSize: 16 }).toDataURL()));

Output

<img alt='kibo' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABmJLR0QA/wD/AP+gvaeTAAAA7UlEQVR4nO3asQ3CQBAAwX9kiVqogP5DV0AtRFDEBCfDTm6/vbrodfv5un/WoPPx3vL89PffJg//BQVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUR7+j7t6ppAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQES7eWv5ft70fqCe3wSiAqICogKiAqICogKiAqICogKiAqICogKiAqICouPf9wP1/5tAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQHToC66+39d94LACogKiAqICogKiAqICogKiAqICogKiAqICogKiL2SGHPJ+jTmVAAAAAElFTkSuQmCC' />

kibo

API

identicon(str, pixelSize, border)

  • str: string - username, email, or other string to hash
  • opts: object
    • pixelSize: int (default: 10) - width and height in pixels to render each tile
    • bgColor: mixed (default: null) - color to fill background
    • null for transparent
    • 0 or 1 for first (brighter) or second (darker) random color
    • CSS hex colorspec (e.g. '#F0F0F0')
    • pixelPadding: int (default: 0) - background pixels within the border of each rendered tile. Use negative values for overlap, as in Github- style identicons
    • imagePadding: int (default: 0) - padding around outside of image
    • tiles: int (default: 5) - number of tiles wide and high to render
    • minFill: float (default: 0.3) - proportion of tiles which must be filled. Hash chaining is used to satisfy fill criteria.
    • maxFill: float (default: 0.9) - maximum proportion of tiles which may be filled.
    • pixelColor: mixed (default: 0) - color to fill foreground tiles. All bgColor values are valid for pixelColor.

返回包含渲染图像的 Canvas 对象。

Styles

所有样式选项都可以更改,但 identicon 带有几个预卷 样式:

default

透明背景下的彩色瓷砖。

default

mini

带有填充图块的微型 3x3 标识。

mini

gravatar

不是 Gravatar 的“复古”风格的完美复制品,但很近。

gravatar

mono

灰色背景上的黑色方块。

mono

mosaic

填充图块,用于马赛克效果。

mosaic

window

彩色背景下的透明填充图块。

window

github

Github identicon 风格的忠实复制品。

github

custom example

荒谬的设置,例如: { pixelSize: 8, pixelPadding: -2, tiles: 30, pixelColor: 0, bgColor: 1 } 仍然可以产生非常有用的结果。

bat

Algorithm

输入字符串转换为UTF-的缓冲区8 个字节。 单字节迭代器 附加到此,初始化为零,用于哈希链接。 缓冲区是 通过 SHA-512 传递,并且输出缓冲区被迭代异或以产生一个 最终缓冲区只有所需字节数(目前为 18 个)。 这 前六个字节作为颜色的 RGB 值。 剩余字节 在 127 的阈值处被量化为布尔值。如果最小比例 的布尔值是真的,但不超过最大比例,散列是 公认。 否则,迭代器递增,并重复该过程。

布尔数组被认为是覆盖左半部分的行优先数组 的最终图像。 它反映在中心垂直轴(或列, 对于奇数顺序数组),然后将其呈现为最终的视觉散列。

Known Limitations

  • 30x30 tile images are the maximum which can be generated from SHA-512.

node-identicon-github

Dependency Status

The author of https://github.com/sehrgut/node-retricon has not maintain module for a long time (since 2013). Some dependencies has been updated and we need to use it.

Create indenticon-like image hashes styled like Github and Gravatar (retro) avatars

avatar avatar avatar avatar avatar avatar skull frog

Example

var identicon = require('identicon-github');
var fmt = require('util').format;

console.log(fmt("<img alt='kibo' src='%s' />", identicon('kibo', { pixelSize: 16 }).toDataURL()));

Output

<img alt='kibo' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAABmJLR0QA/wD/AP+gvaeTAAAA7UlEQVR4nO3asQ3CQBAAwX9kiVqogP5DV0AtRFDEBCfDTm6/vbrodfv5un/WoPPx3vL89PffJg//BQVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUR7+j7t6ppAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQES7eWv5ft70fqCe3wSiAqICogKiAqICogKiAqICogKiAqICogKiAqICouPf9wP1/5tAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQFRAVEBUQHToC66+39d94LACogKiAqICogKiAqICogKiAqICogKiAqICogKiL2SGHPJ+jTmVAAAAAElFTkSuQmCC' />

kibo

API

identicon(str, pixelSize, border)

  • str: string - username, email, or other string to hash
  • opts: object
    • pixelSize: int (default: 10) - width and height in pixels to render each tile
    • bgColor: mixed (default: null) - color to fill background
    • null for transparent
    • 0 or 1 for first (brighter) or second (darker) random color
    • CSS hex colorspec (e.g. '#F0F0F0')
    • pixelPadding: int (default: 0) - background pixels within the border of each rendered tile. Use negative values for overlap, as in Github- style identicons
    • imagePadding: int (default: 0) - padding around outside of image
    • tiles: int (default: 5) - number of tiles wide and high to render
    • minFill: float (default: 0.3) - proportion of tiles which must be filled. Hash chaining is used to satisfy fill criteria.
    • maxFill: float (default: 0.9) - maximum proportion of tiles which may be filled.
    • pixelColor: mixed (default: 0) - color to fill foreground tiles. All bgColor values are valid for pixelColor.

Returns a Canvas object containing the rendered image.

Styles

All style options can be changed, but identicon comes with several prerolled styles:

default

Coloured tiles against a transparent background.

default

mini

Tiny 3x3 identicons with padded tiles.

mini

gravatar

Not a perfect replica of Gravatar's "retro" style, but close.

gravatar

mono

Black tiles on a grey background.

mono

mosaic

Padded tiles, for a mosaic effect.

mosaic

window

Transparent, padded tiles against coloured background.

window

github

Faithful replica of Github's identicon style.

github

custom example

Absurd settings, such as: { pixelSize: 8, pixelPadding: -2, tiles: 30, pixelColor: 0, bgColor: 1 } can still yield quite serviceable results.

bat

Algorithm

The input string is converted to a buffer of UTF-8 bytes. A one-byte iterator is appended to this, initialized at zero, for hash chaining. The buffer is passed through SHA-512, and the output buffer is iteratively XORed to produce a final buffer of only the number of required bytes (currently eighteen). The first six bytes are taken as RGB values for the colours. The remaining bytes are quantized to boolean values at a threshold of 127. If a minimum proportion of the booleans are true, but not more than the maximum proportion, the hash is accepted. Otherwise, the iterator is incremented, and the process repeated.

The array of booleans is taken to be a row-major array covering the left half of the final image. It is reflected about the central vertical axis (or column, for odd-order arrays) which is then rendered as the final visual hash.

Known Limitations

  • 30x30 tile images are the maximum which can be generated from SHA-512.
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文