2010-01-20 124 views

回答

88

構建URL時,請使用參數d = 404。如果用戶沒有設置圖片,這將導致Gravatar返回404錯誤而不是圖像。

如果您使用從gravitar網站鏈接的.Net控件,則需要修改IconSet枚舉(並且可能將代碼拉出控件,以便您可以直接進入狀態)。

+4

- > http://www.gravatar.com/avatar/aaa?default=404 – thijs 2010-01-27 16:34:47

+0

這比其他所有答案都要乾淨得多。喜歡它 - 支付閱讀它看起來的文件。 – Paddy 2010-01-28 16:51:49

+0

一旦你有了404,你如何將它變成gravatar = false? – woodenfox 2015-10-21 23:18:47

6

我所做的:

  • 生成的gravatar與非現有的電子有電子郵件地址
  • 保存圖像
  • 製作的圖像內容的MD5校驗和,並將其存儲在一個恆定的你應用程序代碼

之後,我這樣做是爲了每一個的gravatar要求:

  • 下載的gravatar形象
  • MD5校驗內容,如果匹配它的默認圖像,如果沒有它是一個自定義圖像

我也緩存到它比較對恆

  • gravatar image 24小時,所以你不必一直依賴gravatar。可選地,您可以將前3個點放在一個函數中,讓它稍後運行一次,以確保gravatar仍然使用相同的默認圖像,儘管它們至少在過去的幾個月中還沒有(從不)。

  • +0

    還不錯idea..What是MD5校驗? – Luke101 2010-01-24 18:15:55

    +0

    MD5校驗和是文件完整內容的MD5散列值; C#代碼片段:http:// sharpertutorials。com/calculate-md5-checksum-file/ – fijter 2010-01-24 18:55:56

    +0

    選擇的答案更好,但是這個技巧在某些情況下很有用。例如,如果您不希望瀏覽器在開發人員工具中記錄「404未找到」錯誤,那麼您可以在網頁或瀏覽器擴展程序的Javascript中使用。 – 2016-01-14 18:06:27

    3

    在PHP:

    function hasGravatar($email) 
    { 
        return (md5(file_get_contents(sprintf('http://www.gravatar.com/avatar/%s?default=identicon&size=32', md5($email)))) == '02dcccdb0707f1c5acc9a0369ac24dac') ? false : true; 
    } 
    
    +0

    有沒有一個在線代碼翻譯將它變成c#? – Luke101 2010-01-25 04:43:52

    +0

    @ Luke101:不知道,我仍然在C#的第一步。抱歉。 – 2010-01-25 05:05:20

    +0

    爲什麼平等檢查到02dcccdb0707f1c5acc9a0369ac24dac? – adrianbanks 2010-01-26 23:21:19

    2

    在C#中,根據公佈較早的PHP代碼(未經測試 - 預午餐源代碼如下):

    using System; 
    using System.Text; 
    using System.Security.Cryptography; 
    using System.IO; 
    using System.Net.WebClient; 
    
    public string GenerateMD5(string plaintext) 
    { 
        Byte[] _originalBytes; 
        Byte[] _encodedBytes; 
        MD5 _md5; 
    
        _md5 = new MD5CryptoServiceProvider(); 
        _originalBytes = ASCIIEncoding.Default.GetBytes(plaintext); 
        _encodedBytes = _md5.ComputeHash(_originalBytes); 
    
        return BitConverter.ToString(_encodedBytes).ToLower(); 
    } 
    
    public string file_get_contents(string url) 
    { 
        string sContents = string.Empty; 
    
        if (url.ToLower().IndexOf("http:") > -1) { 
         System.Net.WebClient wc = new System.Net.WebClient(); 
         byte[] response = wc.DownloadData(url); 
         sContents = System.Text.Encoding.ASCII.GetString(response); 
        } else { 
         System.IO.StreamReader sr = new System.IO.StreamReader(url); 
         sContents = sr.ReadToEnd(); 
         sr.Close(); 
        } 
    
        return sContents; 
    } 
    
    public bool hasGravatar(string email) 
    { 
        string _mailMD5 = GenerateMD5(email); 
        string _url = String.Format("http://www.gravatar.com/avatar/{0}?default=identicon&size=32", _mailMD5); 
        string _fileMD5 = GenerateMD5(file_get_contents(_url)); 
    
        return !(_fileMD5 == "02dcccdb0707f1c5acc9a0369ac24dac"); 
    } 
    
    1

    目前我在做類似的事情。我有一個用戶配置文件的表設置,並在該表中我有一列名爲阿凡達。這是一個Gravatar URL將被存儲的地方。以下代碼是我用來管理此列的內容。

    // first gather the email address that is going to be associated with this user as 
    // their gravatar. 
    // once you have gathered the email address send it to a private method that 
    // will return the correct url format. 
    protected void uxAssocateAvatar_Click(object sender, EventArgs e) 
    { 
    if (Page.IsValid) 
    { 
        string emailAddress = uxEmailAddress.Text; 
        try 
        { 
         Profile.Avatar = GetGravatarUrl(emailAddress); 
         Profile.Save(); 
         Response.Redirect("Settings.aspx", true); 
        } 
        catch (Exception ex) 
        { 
         ProcessException(ex, Page); 
        } 
    } 
    } 
    
    // use this private method to hash the email address, 
    // and then create the url to the gravatar service. 
    private string GetGravatarUrl(string dataItem) 
    { 
        string email = dataItem; 
        string hash = 
         System.Web.Security.FormsAuthentication. 
         HashPasswordForStoringInConfigFile(email.Trim(), "MD5"); 
        hash = hash.Trim().ToLower(); 
        string gravatarUrl = string.Format(
         "http://www.gravatar.com/avatar.php?gravatar_id={0}&rating=G&size=100", 
         hash); 
        return gravatarUrl; 
    } 
    
    // on the page where an avatar will be displayed, 
    // just drop in an asp.net image control with a default image. 
    <asp:Image ID="uxAvatar" runat="server" ImageUrl="~/images/genericProfile.jpg" 
    AlternateText="" CssClass="profileAvatar" BorderWidth="1px"/> 
    
    // and on page_load or something like that, 
    // check to see if the profile's avatar property is set 
    if (Profile.Avatar != null) 
    { 
        uxAvatar.ImageUrl = Profile.Avatar; 
    } 
    
    // by default the profile's avatar property will be null, and when a user decides 
    // that they no longer want an avatar, the can de-associate it by creating a null 
    // property which can be checked against 
    // to see if they have one or don't have one. 
    protected void uxRemoveAvatar_Click(object sender, EventArgs e) 
    { 
        Profile.Avatar = null; 
        Profile.Save(); 
        Response.Redirect("Settings.aspx", true); 
    } 
    

    這似乎對我來說工作得很好。我總是有一個默認的頭像顯示,當用戶真的想要顯示他們的自定義頭像時,他們將他們的Gravatar電子郵件(我散列並從不存儲爲電子郵件地址)關聯起來,創建一個URL,我可以將其作爲imageURL 。當用戶刪除他們的gravatar鏈接時,我將數據庫列清空,並且imageURL返回到我的默認圖像。

    祝你好運,並希望這可以幫助你一些。

    1
    private bool HasUserPublicGravatar(string email) 
        { 
         try 
         { 
          var gravatarPath = GravatarService.GetGravatarUrlForAddress(email, 
          new GravatarUrlParameters { DefaultOption = GravatarDefaultUrlOptions.Error }); 
    
          WebRequest wReq = HttpWebRequest.Create(gravatarPath); 
          var wRes = wReq.GetResponse(); 
          return true; 
         } 
         catch (System.Net.WebException ex) 
         { 
          if (ex.Message.Contains("404")) 
           return false; 
          else 
           throw new Exception("Couldn't determine if ueser has public avatar"); 
         } 
        } 
    
    0
    function get_gravatar($email, $s = 80, $d = '404', $r = 'x', $img = false, $atts = array()) { 
    
    $url = 'http://www.gravatar.com/avatar/'; 
    $url .= md5(strtolower(trim($email))); 
    $url .= "?s=$s&d=$d&r=$r"; 
    
    if ($img) 
    { 
    $url = '<img src="' . $url . '"'; 
    foreach ($atts as $key => $val) 
    $url .= ' ' . $key . '="' . $val . '"'; 
    $url .= ' />'; 
    return $url; 
    
    } 
    
    $headers = @get_headers($url); 
    
    if (!preg_match("|200|", $headers[0])) 
    { 
    $has_valid_avatar = 'no'; 
    } 
    else 
    { 
    $has_valid_avatar = 'yes'; 
    } 
    
    return $has_valid_avatar; 
    
    } 
    
    相關問題