identicon
identicon.cr
A Crystal implementation of ruby_identicon which is a Ruby implementation of go-identicon.
identicon.cr
creates an identicon, similar to those created by Github. Unfortunately the algorithm Github uses isn't open source, so this isn't the exact same.
A title and key are used by siphash to calculate a hash value that is then used to create a visual identicon representation. The identicon is made by creating a left hand side pixel representation of each bit in the hash value - this is then mirrored onto the right hand side to create an image that we see as a shape. The grid and square sizes can be varied to create identicons of differing size.
Installation
-
Add the dependency to your
shard.yml
:dependencies: identicon: github: watzon/identicon
-
Run
shards install
Usage
First require the library:
require "identicon"
You can easily generate and save a png identicon:
Identicon.create_and_save("myemailaddress@gmail.com", "sample_01.png")
Which creates an identicon that looks like this:
If you don't want to save a file, you can also generate an IO
:
Identicon.create("myemailaddress@gmail.com")
This returns an IO::Memory
containing the raw PNG bytes. If you wish to export a base64 encoded string you can do that too:
Identicon.create_base64("myemailaddress@gmail.com")
Options
All of the create methods take a number of different options that can be used to change the appearance of the generated image. The options are:
key
A 16 byte key to be passed to the siphash digest.
grid_size
The number of rows and columns in the identicon. A Github identicon is 5x5, but we default to 7x7.
border_size
The size in pixels to leave as an empty border around the identicon image. Defaults to 35.
square_size
The size in pixels of each square that makes up the identicon. Defaults to 50.
background_color
The StumpyPNG::RGBA
color to use as the image background.
bit_depth
The bit depth of the generated image. Defaults to 8.
color_type
The color type of the generated image. Defaults to :rgb_alpha
.
Contributing
- Fork it (https://github.com/watzon/identicon/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Chris Watson - creator and maintainer
identicon
- 8
- 0
- 0
- 1
- 2
- over 4 years ago
- May 21, 2020
MIT License
Thu, 21 Nov 2024 21:27:05 GMT