10

GravatarサービスのAPI(まあ、それは実際には単なるURLです)は非常に単純ですが、Gravatarで利用可能なすべてのオプションをうまく反映する簡単なヘルパーメソッドはありますか?

  • 画像サイズ
  • デフォルトの画像(ユーザーが指定していない場合)
  • 評価(G / PG / R / X)

理想的には、これはHtmlHelper拡張メソッドになります。

私は.NET4でMVC2をターゲットにしていますが、他の人も以前のバージョンのオプションに興味があると思います。

EDIT実装では、生成されたHTML要素にも追加の属性を提供できるようにする必要があります。

4

5 に答える 5

20

回答ありがとうございます。最後に、私は独自のソリューションを作成したので、役に立つと思われる他の人のためにここに投稿します。

質問に記載されているように、Gravatar が現在サポートしているすべての機能に対応しています。

次のように使用します。

<%= Html.Gravatar(Model.User.EmailAddress) %>

オプションの引数を任意のオプションに指定しました。これらは組み合わせることができます。

// Use a specific image size (the default is 80px)
Html.Gravatar(Model.User.EmailAddress, size:64)

// Specify what image should appear if the email address is not
// associated with a Gravatar account
Html.Gravatar(Model.User.EmailAddress,
              defaultImage:GravatarDefaultImage.Identicon)

// Specify the maximum rating allowed for images
Html.Gravatar(Model.User.EmailAddress, rating:GravatarRating.Pg)

// Add any additional HTML attributes for the <img /> tag
Html.Gravatar(Model.User.EmailAddress,
              htmlAttributes:new { @class = "gravatar" })

コードは次のとおりです。

using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace SampleNamespace
{
    public static class HtmlHelperGravatar
    {
        /// <summary>
        /// Creates HTML for an <c>img</c> element that presents a Gravatar icon.
        /// </summary>
        /// <param name="html">The <see cref="HtmlHelper"/> upon which this extension method is provided.</param>
        /// <param name="email">The email address used to identify the icon.</param>
        /// <param name="size">An optional parameter that specifies the size of the square image in pixels.</param>
        /// <param name="rating">An optional parameter that specifies the safety level of allowed images.</param>
        /// <param name="defaultImage">An optional parameter that controls what image is displayed for email addresses that don't have associated Gravatar icons.</param>
        /// <param name="htmlAttributes">An optional parameter holding additional attributes to be included on the <c>img</c> element.</param>
        /// <returns>An HTML string of the <c>img</c> element that presents a Gravatar icon.</returns>
        public static string Gravatar(this HtmlHelper html,
                                      string email, 
                                      int? size = null,
                                      GravatarRating rating = GravatarRating.Default,
                                      GravatarDefaultImage defaultImage = GravatarDefaultImage.MysteryMan,
                                      object htmlAttributes = null)
        {
            var url = new StringBuilder("//www.gravatar.com/avatar/", 90);
            url.Append(GetEmailHash(email));

            var isFirst = true;
            Action<string,string> addParam = (p,v) =>
                {
                    url.Append(isFirst ? '?' : '&');
                    isFirst = false;
                    url.Append(p);
                    url.Append('=');
                    url.Append(v);
                };

            if (size != null)
            {
                if (size < 1 || size > 512)
                    throw new ArgumentOutOfRangeException("size", size, "Must be null or between 1 and 512, inclusive.");
                addParam("s", size.Value.ToString());
            }

            if (rating != GravatarRating.Default)
                addParam("r", rating.ToString().ToLower());

            if (defaultImage != GravatarDefaultImage.Default)
            {
                if (defaultImage==GravatarDefaultImage.Http404)
                    addParam("d", "404");
                else if (defaultImage==GravatarDefaultImage.Identicon)
                    addParam("d", "identicon");
                if (defaultImage==GravatarDefaultImage.MonsterId)
                    addParam("d", "monsterid");
                if (defaultImage==GravatarDefaultImage.MysteryMan)
                    addParam("d", "mm");
                if (defaultImage==GravatarDefaultImage.Wavatar)
                    addParam("d", "wavatar");
            }

            var tag = new TagBuilder("img");
            tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            tag.Attributes.Add("src", url.ToString());

            if (size!=null)
            {
                tag.Attributes.Add("width", size.ToString());
                tag.Attributes.Add("height", size.ToString());
            }

            return tag.ToString();
        }

        private static string GetEmailHash(string email)
        {
            if (email == null)
                return new string('0', 32);

            email = email.Trim().ToLower();

            var emailBytes = Encoding.ASCII.GetBytes(email);
            var hashBytes = new MD5CryptoServiceProvider().ComputeHash(emailBytes);

            Debug.Assert(hashBytes.Length == 16);

            var hash = new StringBuilder();
            foreach (var b in hashBytes)
                hash.Append(b.ToString("x2"));
            return hash.ToString();
        }
    }

    public enum GravatarRating
    {
        /// <summary>
        /// The default value as specified by the Gravatar service.  That is, no rating value is specified
        /// with the request.  At the time of authoring, the default level was <see cref="G"/>.
        /// </summary>
        Default,

        /// <summary>
        /// Suitable for display on all websites with any audience type.  This is the default.
        /// </summary>
        G,

        /// <summary>
        /// May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.
        /// </summary>
        Pg,

        /// <summary>
        /// May contain such things as harsh profanity, intense violence, nudity, or hard drug use.
        /// </summary>
        R,

        /// <summary>
        /// May contain hardcore sexual imagery or extremely disturbing violence.
        /// </summary>
        X
    }

    public enum GravatarDefaultImage
    {
        /// <summary>
        /// The default value image.  That is, the image returned when no specific default value is included
        /// with the request.  At the time of authoring, this image is the Gravatar icon.
        /// </summary>
        Default,

        /// <summary>
        /// Do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response.
        /// </summary>
        Http404,

        /// <summary>
        /// A simple, cartoon-style silhouetted outline of a person (does not vary by email hash).
        /// </summary>
        MysteryMan,

        /// <summary>
        /// A geometric pattern based on an email hash.
        /// </summary>
        Identicon,

        /// <summary>
        /// A generated 'monster' with different colors, faces, etc.
        /// </summary>
        MonsterId,

        /// <summary>
        /// Generated faces with differing features and backgrounds.
        /// </summary>
        Wavatar
    }
}
于 2010-08-24T23:36:51.590 に答える
12

プロジェクトでこれが必要だったので、http: //www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/を使用しました。

そしてそれをこれに変えました:

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";
    }

    /// <summary>
    /// The Email for the user
    /// </summary>

    public string Email
    {
        get
        {
            return _email;
        }

        set
        {
            _email = value.ToLower();
        }
    }

    /// <summary>
    /// Size of Gravatar image.  Must be between 1 and 512.
    /// </summary>
    public short Size { get; set; }

    /// <summary>
    /// An optional "rating" parameter may follow with a value of [ G | PG | R | X ] that determines the highest rating (inclusive) that will be returned.
    /// </summary>
    public RatingType MaxAllowedRating { get; set; }

    /// <summary>
    /// Determines whether the image is wrapped in an anchor tag linking to the Gravatar sit
    /// </summary>
    public bool OutputGravatarSiteLink { get; set; }

    /// <summary>
    /// Optional property for link title for gravatar website link
    /// </summary>
    public string LinkTitle { get; set; }

    /// <summary>
    /// 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.
    /// </summary>
    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 < 1 || Size > 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 < hashedBytes.Length; i++)
            {
                sb.Append(hashedBytes[i].ToString("X2"));
            }

            // output parameters:
            imageUrl += "gravatar_id=" + sb.ToString().ToLower();
            imageUrl += "&rating=" + MaxAllowedRating.ToString();
            imageUrl += "&size=" + Size.ToString();
        }

        // output default parameter if specified
        if (!string.IsNullOrEmpty(DefaultImage))
        {
            imageUrl += "&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;

    }
}

使用法:

<%= Html.Gravatar("john.e.farrell@gmail.com", true)%>

必要に応じてオーバーロードを html ヘルパーに追加する必要があります。私はすべての仕事をすることはできません。;) 5分間は悪くないですよね?

于 2010-08-24T23:11:15.480 に答える
4

以下にリンクされている Rob Conery の gravatar ヘルパー拡張機能は、良い出発点になるでしょう。

ASP.NET MVC のお気に入りのヘルパー

サイトのコード:

public static class GravatarHelper {

    public static string Gravatar(this HtmlHelper helper, string email, int size) {
        var result = "<img src=\"{0}\" alt=\"Gravatar\" class=\"gravatar\" />";
        var url = GetGravatarURL(email, size);
        return string.Format(result, url);
    }

    static string GetGravatarURL(string email, int size) {
        return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=PG", 
                    EncryptMD5(email), size.ToString()));
    }

    static string GetGravatarURL(string email, int size, string defaultImagePath) {
        return GetGravatarURL(email, size) + string.Format("&default={0}", 
                   defaultImagePath);
    }

    static string EncryptMD5(string Value) {
        var md5 = new MD5CryptoServiceProvider();
        var valueArray = System.Text.Encoding.ASCII.GetBytes(Value);
        valueArray = md5.ComputeHash(valueArray);
        var encrypted = "";
        for (var i = 0; i < valueArray.Length; i++)
            encrypted += valueArray[i].ToString("x2").ToLower();
        return encrypted;
    }
}

HTH、
チャールズ

于 2010-08-24T23:11:43.817 に答える
2

残念ながら、あなたの質問を以前に見たことがありませんでした。または、Gravatar API 全体の .NET ラッパーを提供するために構築した Gravatar.NET ライブラリを使用することをお勧めしました。

興味がある場合は、こちらをご覧ください: http://gravatarnet.codeplex.com/

于 2010-09-10T22:40:19.297 に答える