Skip to content
Create github-style identicons that can be used as avatars
Crystal
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
spec
src
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
shard.yml

README.md

identicon.cr

Create github-style identicons

This is a straight port of Chris Branson's ruby_identicon, which is a Ruby implementation of go-identicon by Damian Gryski

Installation

Add this to your application's shard.yml:

dependencies:
  identicon:
    github: oneiros/identicon.cr

Usage

require "identicon"

Creating an identicon and saving to png

Identicon.create_and_save("Identicon", "identicon.png")

Creating an identicon and returning a binary string

blob = Identicon.create("Identicon")

# optional, save to a file
File.open("identicon.png", "wb") do |f|
  f << blob
end

Creating an identicon and returning in Base64 format

base64_identicon = Identicon.create_base64("Identicon")

to render this in HTML, pass the Base64 code into your template:

<img src='data:image/png;base64,<%= base64_identicon %>'>

Customising the identicon

The identicon can be customised by passing additional options

background_color:  (String, default "#00000000") the background color of the identicon in hex notation (e.g. "#ffffff" for white)
border_size:  (Int32, default 35) the size in pixels to leave as an empty border around the identicon image
grid_size:    (Int32, default 7)  the number of rows and columns in the identicon, minimum 4, maximum 9
square_size:  (Int32, default 50) the size in pixels of each square that makes up the identicon
key:          (String) a 16 byte key used by siphash when calculating the hash value (see note below)

Varying the key ensures uniqueness of an identicon for a given title, it is assumed desirable for different applications to use a different key.

Example

blob = Identicon.create("identicons are great!", grid_size: 5, square_size: 70, background_color: "#f0f0f0ff", key: "1234567890123456")
File.open("tmp/test_identicon.png", "wb") { |f| f << blob }

Contributing

  1. Fork it (https://github.com/oneiros/identicon/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • oneiros David Roetzel - creator, maintainer

With many thanks to:

  • chrisbranson Chris Branson - for ruby_identicon
  • dgryski Damian Gryski - for the original golang implementation
  • ysbaddaden Julien Portalier - for the excellent crystal siphash library
  • l3kn Leon Rische - for stumpy_png
You can’t perform that action at this time.