虽然Gravatar服务的API(嗯,它实际上只是一个URL)非常简单,有没有一个简单的帮助方法可以很好地反映Gravatar的所有可用选项?
理想情况下,这将是HtmlHelper
扩展方法。
我的目标是在.NET4上使用MVC2,但我想其他人也会对早期版本的选项感兴趣。
编辑实现应该允许为生成的HTML元素提供其他属性。
答案 0 :(得分:19)
感谢您的回答。最后我写了自己的解决方案,所以我会在这里发布给其他可能觉得有用的人。
它满足了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
}
}
答案 1 :(得分: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分钟呃?
答案 2 :(得分:4)
Rob Conery在下面链接的重力级助手扩展将是一个良好的开端。
My Favorite Helpers For 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;
}
}
HTHS,
查尔斯
答案 3 :(得分:2)
不幸的是,我之前没有看到您的问题,或者我建议您使用我构建的Gravatar.NET库来为整个Gravatar API提供.NET包装。
如果您有兴趣,请在此处查看:http://gravatarnet.codeplex.com/
答案 4 :(得分:-1)
如果有兴趣的话,这是一个.Net Core Tag Helper:
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace App.Web.TagHelpers
{
public class GravatarTagHelper : TagHelper
{
public string Id { get; set; }
public string Email { get; set; }
public int? Size { get; set; }
public string Class { get; set; }
public string Alt { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "img";
if (!string.IsNullOrWhiteSpace(Id))
{
output.Attributes.SetAttribute("id", Id);
}
output.Attributes.SetAttribute("alt", Alt);
output.Attributes.SetAttribute("src", GenerateGravatarUrl(Email));
if (!string.IsNullOrWhiteSpace(Class))
{
output.Attributes.SetAttribute("class", GenerateGravatarUrl(Class));
}
if (Size.HasValue)
{
output.Attributes.SetAttribute("width", Size.ToString());
output.Attributes.SetAttribute("height", Size.ToString());
}
}
private static string GenerateGravatarUrl(string email)
{
var md5 = MD5.Create();
var encoder = new UTF8Encoding();
var md5Hash = md5.ComputeHash(encoder.GetBytes(email.Trim().ToLower()));
var md5String = new StringBuilder(md5Hash.Length * 2);
foreach (var character in md5Hash)
{
md5String.Append(character.ToString("X2"));
}
return $"https://www.gravatar.com/avatar/{md5String.ToString().ToLower()}?d=mm";
}
}
}