Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I needed this for a project so I took: <a href="http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/" rel="noreferrer">http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/</a></p> <p>and turned it into this:</p> <pre><code>public static class GravatarHtmlHelper { public static Gravatar Gravatar( this HtmlHelper htmlHelper, string email ) { return new Gravatar() {Email = email, Size = 50, MaxAllowedRating = WebControls.Gravatar.RatingType.X}; } public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, bool outputSiteLink) { return new Gravatar() { Email = email, Size = 50, MaxAllowedRating = WebControls.Gravatar.RatingType.X, OutputGravatarSiteLink = outputSiteLink }; } public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, short size ) { return new Gravatar() { Email = email, Size = size, MaxAllowedRating = WebControls.Gravatar.RatingType.X }; } public static Gravatar Gravatar(this HtmlHelper htmlHelper, string email, short size, bool outputSiteLink) { return new Gravatar() { Email = email, Size = size, MaxAllowedRating = WebControls.Gravatar.RatingType.X, OutputGravatarSiteLink = outputSiteLink}; } } public class Gravatar { public enum RatingType { G, PG, R, X } private string _email; // outut gravatar site link true by default: // customise the link title: public Gravatar() { OutputGravatarSiteLink = true; LinkTitle = "Get your avatar"; } /// &lt;summary&gt; /// The Email for the user /// &lt;/summary&gt; public string Email { get { return _email; } set { _email = value.ToLower(); } } /// &lt;summary&gt; /// Size of Gravatar image. Must be between 1 and 512. /// &lt;/summary&gt; public short Size { get; set; } /// &lt;summary&gt; /// An optional "rating" parameter may follow with a value of [ G | PG | R | X ] that determines the highest rating (inclusive) that will be returned. /// &lt;/summary&gt; public RatingType MaxAllowedRating { get; set; } /// &lt;summary&gt; /// Determines whether the image is wrapped in an anchor tag linking to the Gravatar sit /// &lt;/summary&gt; public bool OutputGravatarSiteLink { get; set; } /// &lt;summary&gt; /// Optional property for link title for gravatar website link /// &lt;/summary&gt; public string LinkTitle { get; set; } /// &lt;summary&gt; /// An optional "default" parameter may follow that specifies the full, URL encoded URL, protocol included, of a GIF, JPEG, or PNG image that should be returned if either the requested email address has no associated gravatar, or that gravatar has a rating higher than is allowed by the "rating" parameter. /// &lt;/summary&gt; public string DefaultImage { get; set; } public override string ToString() { // if the size property has been specified, ensure it is a short, and in the range // 1..512: try { // if it's not in the allowed range, throw an exception: if (Size &lt; 1 || Size &gt; 512) throw new ArgumentOutOfRangeException(); } catch { Size = 80; } // default the image url: string imageUrl = "http://www.gravatar.com/avatar.php?"; if (!string.IsNullOrEmpty(Email)) { // build up image url, including MD5 hash for supplied email: MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Email)); StringBuilder sb = new StringBuilder(hashedBytes.Length * 2); for (int i = 0; i &lt; hashedBytes.Length; i++) { sb.Append(hashedBytes[i].ToString("X2")); } // output parameters: imageUrl += "gravatar_id=" + sb.ToString().ToLower(); imageUrl += "&amp;rating=" + MaxAllowedRating.ToString(); imageUrl += "&amp;size=" + Size.ToString(); } // output default parameter if specified if (!string.IsNullOrEmpty(DefaultImage)) { imageUrl += "&amp;default=" + HttpUtility.UrlEncode(DefaultImage); } var linkBuilder = new TagBuilder("a"); // if we need to output the site link: if (OutputGravatarSiteLink) { linkBuilder.MergeAttribute("href", "http://www.gravatar.com"); linkBuilder.MergeAttribute("title", LinkTitle); } // output required attributes/img tag: var builder = new TagBuilder("img"); builder.MergeAttribute("width", Size.ToString()); builder.MergeAttribute("height", Size.ToString()); builder.MergeAttribute("src", imageUrl); builder.MergeAttribute("alt", "Gravatar"); string output = builder.ToString(TagRenderMode.Normal); // if we need to output the site link:) if (OutputGravatarSiteLink) { linkBuilder.InnerHtml = builder.ToString(); output = linkBuilder.ToString(TagRenderMode.Normal); } return output; } } </code></pre> <p>Usage:</p> <pre><code>&lt;%= Html.Gravatar("john.e.farrell@gmail.com", true)%&gt; </code></pre> <p>You'll have to add the overloads to the html helper as you need them. I can't do all the work. ;) Not bad for 5 min eh?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload