Skip to content

Instantly share code, notes, and snippets.

@conficient
Last active June 10, 2022 11:47

Revisions

  1. conficient revised this gist Jun 10, 2022. No changes.
  2. conficient created this gist Jun 10, 2022.
    20 changes: 20 additions & 0 deletions Gravatar.razor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    @* Creates a Gravatar image using Bootstrap styling from email address *@
    <img class="rounded-circle" src="@url" />
    @code
    {
    [Parameter] public string Email { get; set; }
    [Parameter] public int Size { get; set; } = 24;

    protected override void OnParametersSet()
    {
    // create hash
    var bytes = System.Text.Encoding.UTF8.GetBytes(Email.ToLower());
    var hash = System.Security.Cryptography.MD5.HashData(bytes);
    var hex = string.Join("", hash.Select(x => x.ToString("x2")));
    url = $"https://www.gravatar.com/avatar/{hex}?s={sizeClean}";
    }

    const int minSize = 8;
    private int sizeClean => Size>= minSize ? Size: minSize;
    private string url = null;
    }