PHP Gravatar Class

Lucas Araujo wrote in to share his PHP gravatar class. Its fine work, and definitely worth a look (not to mention yet another example of the community really being the heart and soul of a project like this!)

Subscribe and get notified about the latest Gravatar news…

Join 15.3K other subscribers


26 responses to “PHP Gravatar Class”

  1. tikabanget™ Avatar

    let’s take a look.. ^^


  2. ChaosKaizer Avatar

    nice simple php5 OO class but lack caching & semantic img tag (missing alt attributes).


  3. Dhruva Sagar Avatar

    Gravatars has been gaining popularity in recent times, their APIs are pretty easy to tap in, this class is a good example of a well written OO way of accessing and using the APIs. Nice work.
    I think caching can be managed at the implementation end, there is no need for caching to be part of the class, the class’ sole objective is to provide and abstraction from the gravatar APIs so that we can access and get the various gravatars easily. I believe if you write a plugin or something of the sort, you should work on the caching part there and not in this class itself. Atleast that is what I think…


  4. Lucas Araujo Avatar


    You can use setExtra(”) to set alt attributes for now.


  5. Coffinboy Avatar

    I must say that I am not impressed, surely a class like this could be useful, but kind of bloated since a tiny little function would suffice, it would be much faster too. And the usage of interceptors seems weird too. Also it returns a img tag without alt attribute as ChaosKaizer said. I’d personally return the URL rather than a image. I could probably do this better but I can’t be bothered. Sorry. :/


  6. L'Orso Ciccione Avatar

    It’ a little bit out of topic, but…
    I am using WordPress 2.3.2 for my blog and I have added Gravatar using the plugin Gravatar2 2.7.0 (
    I hoped it works better than theprevious version, but it still have this problem:
    when a user post a comment and he have a gravatar, sometimes it’s not been taken correctly.
    In the cache it says “no gravatar”, if I click on the link it show the image correctly. This is an example:

    is there something wrong with this gravatar or the generated url? Is there any fix for this bug at last?
    Please answer me or tell me where to post this bug for a solution.


  7. carlajc Avatar

    Just wanted you to know I posted an article about Gravatars, which includes links for sign-up, implementation, etc. I’m already getting comments from experienced bloggers who didn’t know about Gravatars, which is kind of surprising.

    Anyway, if you get a chance, please look over the article and see if there’s any other resource links you think I should add. The post will be permanently linked from my Avatar Central resources page, so I expect a lot of people will see it over time. I’d obviously like it to be as comprehensive as it can be.

    Here’s the post:

    Carla Chadwick


  8. Retodon8 Avatar

    I didn’t fully check the script, but it seems the alt attribute is to be entered as a parameter.
    (Maybe the script was updated since this announcement was made.)

    Yay, I see a “strtolower” before the MD5 generation. 🙂
    Many Gravatar-enabled websites fail to recognise that e-mail addresses are sometimes entered with capitalized names, like “” (which may be saved in an auto-complete functionality), resulting in a different MD5, breaking Gravatar functionality.
    It seems this script takes care of that.

    Info on e-mail address capitalization:


  9. Hairie Avatar

    that was awesome..


  10. emil Avatar

    test test


  11. frank Avatar

    quiero mi gravatar


  12. Mares Avatar

    testing to find if my gravatar is displaying as yet (it hasn’t been all evening)


  13. gingerwench Avatar

    I see your gravatar Mares, how long did it take? I registered yesterday, and still am not able to “select” my avatar in the MANAGE page here. Weird. Any help for me out there? lol


  14. kL Avatar

    Please don’t link to It’s a garbage that bullies users into needless registration.


  15. omarm Avatar

    testing again…tq


  16. Ciprian M. Avatar

    I don’t think this is implemented yet, but for some more extent of the gravatar functionality, I need to get also a false value if no gravatar is found for an email. I already have a default avatar system in my app, so I would like to display the gravatars only for those who registered with, and not getting the “G” image for those who didn’t. How can I do that?

    Hint: I was thinking of reading the returned image gravatar, and if contains a string which identifies only the “G” images, that would give me a false value. What do you thing? What would be that common unique string in the G files? Does anyone know?
    PS: I use the cache class feature for Gravatars, so I don’t want to fill up the site space with useless identical “G” images for each user who didn’t register with As a web developer, I think this is critical to save people’s storage space when using the app I distribute.
    Thanks in advance for any thoughts.


  17. Ciprian M. Avatar

    Does anyone know of a Generic Gravatar sign-up script?
    All I find everywhere is word-press related plugins for gravatars… What the H? Word-press is the only thing out there for coders nowadays?


  18. Ciprian M. Avatar

    No comments at all? hmmm… I might start doing it myself… tonight 😀


  19. Ciprian M. Avatar

    Ok, I’m back (talking to no one though…)
    I discovered all the “G” images are identified by this default gravar_id: “d41d8cd98f00b204e9800998ecf8427e”
    Filtering that string with an eregi() helped me keep out of cache all the default “G” images returned for users/emails that don’t have a gravatar defined/registered. Which is a great tip for my first comment above!

    (wouldn’t have been a lot nicer to get this answer from a “gravatar professional/expert”? thank you for the great service and let us do the rest of the job – good strategy lol)


  20. Ciprian M. Avatar

    Well, not all get that id, just those with an empty email address. It would be better to return that default hash for all non-registered emails as well. It doesn’t help anyone to get millions of hashes for the same default image.
    So my suggestion is: if an email doesn’t exist in the database, the hash remain always the same (“d41d8cd98f00b204e9800998ecf8427e”) – that’s the best way for now to control the cash – for all unidentified users it will be cached a single file, and then the same file (blue G image or identicon and so on) will be displayed for all of those users. A lot more economical from the bandwidth and storage point of view!!! Which might be critical to avoid outages, including the servers…

    Does anyone from the team follow this? Please drop a line…


  21. ciprianmp Avatar

    Ok, I worked it around and it works fine.
    I used a TalkPHPGravatar class posted and improved here:
    Now… what I got is: – Gravatarized phpMyChat-Plus – first LiveChat to fully support gravatars!!!
    It will be included in the soon to come release of 1.93 version 🙂

    PS: Who do I announce about gravatars availability in my app? Matt? Apokalyptik? Thehline?
    Thanks for all the resources and hints guys!


  22. Hieu Vo Avatar

    I appreciate your work a lots, ciprianmp 😀


    1. ciprianmp Avatar

      You’re welcome. I know… after 1 and a half year… sorry.


  23. sean Avatar

    Will it work?


  24. Ricky Hewitt Avatar

    Gravatar is already really simple to use using the PHP reference implementation ( so why not just use that? Creating an entire class to do more or less the same things seems pointless to me.

    Anyone want to elaborate for me as to why such a class is required..?


  25. Laurie Avatar

    checking this out. very cool