Cross-platform extendable library that allows you to create a variety of identicons without any efforts
IdenticonSharp
aims to collect as many identicon generation algorithms as possible, so anyone could customize the default appearance of avatars in a way that he and his users will like. Down with the boring static default avatars!
So if you have an idea to implement a new (or existing one) identicon - you're welcome to fork the project!
Provider
:IdenticonSharp.Identicons.Defaults.GitHub.GitHubIdenticonProvider
Options
:IdenticonSharp.Identicons.Defaults.GitHub.GitHubIdenticonOptions
Yes, I know that QR code is not essentially an identicon... but why not?
Provider
:IdenticonSharp.Identicons.Defaults.QR.QRIdenticonProvider
Options
:IdenticonSharp.Identicons.Defaults.QR.QRIdenticonOptions
Identicons by Google Docs
Provider
:IdenticonSharp.Identicons.Defaults.Animal.AnimalIdenticonProvider
Options
:IdenticonSharp.Identicons.Defaults.Animal.AnimalIdenticonOptions
Since you're likely to use a single type of identicon within the project, IdenticonSharp
provides the IdenticonManager
class, whose Default
property represents the default identicon generator.
All identicon generators implement the IIdenticonProvider
interface, so you can generate an image (or an svg
-document, if the generator supports it) from string
or byte[]
:
string value = ...;
// or
byte[] value = ...;
if (IdenticonManager.Default.ProvidesSvg)
{
var svg = IdenticonManager.Default.CreateSvg(value);
svg.Save("identicon.svg");
}
else
{
var img = IdenticonManager.Default.Create(value);
img.Save("identicon.png");
}
If you need to replace and/or configure the default identicon generator, you can use the ConfigureDefault
method as shown below:
// Passing the type of generator and its options
IdenticonManager.ConfigureDefault<GitHubIdenticonProvider, GitHubIdenticonOptions>(options => {
// Configuring the parameters
options.Background = new Rgba32(240, 240, 240);
options.SpriteSize = 10;
options.Size = 256;
options.HashAlgorithm = HashProvider.SHA512;
});
The same can be done less verbose:
IdenticonManager.ConfigureDefault<GitHubIdenticonProvider>();
// If the type name adheres to the general style, then you can use its short form
IdenticonManager.ConfigureDefault("GitHub");
IdenticonManager.ConfigureDefault<GitHubIdenticonOptions>("GitHub", options => {
...
});
// If the name of the option type adheres to the general style,
// and its short form coincides with the desired generator,
// you can use the following form of method invocation
IdenticonManager.ConfigureDefault<GitHubIdenticonOptions>(options => {
...
});
Also you can use KE.IdenticonSharp.AspNetCore
to interact with ASP.NET Core
projects!
To register IdenticonSharp
as a service, just edit the ConfigureServices
method in your Startup.cs
as follows:
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddIdenticonSharp<GitHubIdenticonProvider, GitHubIdenticonOptions>(options =>
{
// Configuring parameters of default IdenticonProvider
options.SpriteSize = 10;
options.Size = 256;
options.HashAlgorithm = HashProvider.SHA512;
})
.Configure<QRIdenticonOptions>("qr", options =>
{
// Configuring parameters of QRIdenticonProvider's instance
options.Background = new Rgba32(0, 0, 255);
options.Foreground = new Rgba32(0, 255, 0);
options.Border = 40;
options.Scale = 10;
options.CorrectionLevel = CorrectionLevel.High;
options.CenterImage = Image.Load(path);
});
}
As you may have noticed, the AddIdenticonSharp
call is very similar to ConfigureDefault
. And it's true, so this can be written exactly the same shorter:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdenticonSharp<GitHubIdenticonOptions>(options => {
...
}).Configure<QRIdenticonOptions>(options => {
...
});
}
After that you can enjoy all the charms of dependency injection in ASP.NET Core
:
public class HomeController : Controller
{
private readonly IIdenticonProvider IdenticonProvider;
private readonly QRIdenticonProvider QRProvider;
// Framework will pass the configured identicon generators
// to the constructor of your controller
public HomeController(IIdenticonProvider identiconProvider, QRIdenticonProvider qrProvider)
{
// Default IdenticonProvider
IdenticonProvider = identiconProvider;
// Configured QRIdenticonProvider's instance
QRProvider = qrProvider;
}
}
KE.IdenticonSharp.AspNetCore
provides several tag helpers for your convenience.
To import them, simply add the following line to your page (or immediately into the _ViewImports.cshtml
):
@addTagHelper *, KE.IdenticonSharp.AspNetCore
Now you can easily use the following helpers:
@{
string userEmail = ...;
string userSecret = ...;
}
<!-- Generates <img> containing an identicon (by IdenticonManager.Default) -->
<identicon width="256px" height="256px" value="@userEmail">
<!-- Generates <svg> containing an identicon (by IdenticonManager.Default) -->
<identicon width="256px" height="256px" value="@userEmail" svg>
<!-- Generates <img> containing a gravatar -->
<gravatar width="256px" height="256px" value="@userEmail">
<!-- Generates <img> containing a QR code (by configured QRIdenticonProvider instance) -->
<qr width="256px" height="256px" value="@userEmail">
<!-- Generates <svg> containing a QR code (by configured QRIdenticonProvider instance) -->
<qr width="256px" height="256px" value="@userEmail" svg>
<!-- Generates <img> containing a QR code for otpauth (by configured QRIdenticonProvider instance) -->
<!-- (Can be used with Google Authenticator, for example) -->
<!-- https://github.com/google/google-authenticator/wiki/Key-Uri-Format -->
<!-- If encode-secret attribute was not specified, the secret will be encoded only if it contains non-Base32 characters -->
<!-- If encode-secret="true", the secret will be Base32-encoded -->
<!-- If encode-secret="false", the secret will not be Base32-encoded -->
<otp width="256px" height="256px" secret="@userSecret" issuer="My Cool Site" user="@userEmail">
<!-- Generates <svg> containing a QR code for otpauth (by configured QRIdenticonProvider instance) -->
<!-- (Can be used with Google Authenticator, for example) -->
<!-- https://github.com/google/google-authenticator/wiki/Key-Uri-Format -->
<!-- If encode-secret attribute was not specified, the secret will be encoded only if it contains non-Base32 characters -->
<!-- If encode-secret="true", the secret will be Base32-encoded -->
<!-- If encode-secret="false", the secret will not be Base32-encoded -->
<otp width="256px" height="256px" secret="@userSecret" issuer="My Cool Site" user="@userEmail" svg>