PHP Gravatar Class

January 29, 2008 | Posted by Demitrious Kelly

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!)

26 Responses

  1. tikabangetâ„¢ says:

    let’s take a look.. ^^

  2. ChaosKaizer says:

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

  3. Dhruva Sagar says:

    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.
    @ChaosKaize
    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 says:

    Hello,

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

  5. Coffinboy says:

    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. 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 (http://zenpax.com/gravatars2/)
    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 says:

    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: http://b2bcontent.wordpress.com/2008/02/01/gravatars/

    Thanks!
    Carla Chadwick

  8. Retodon8 says:

    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 “Bob@SomeCompany.com” (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:

    http://en.wikipedia.org/wiki/E-mail_address#Limitations

  9. Hairie says:

    that was awesome..

  10. frank says:

    quiero mi gravatar

  11. Mares says:

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

  12. gingerwench says:

    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

  13. kL says:

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

  14. omarm says:

    testing again…tq

  15. Ciprian M. says:

    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 gravatar.com, 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 gravatars.com. 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.

  16. Ciprian M. says:

    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?

  17. Ciprian M. says:

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

  18. Ciprian M. says:

    Ok, I’m back (talking to no one though…)
    Anyway,
    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)

  19. Ciprian M. says:

    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 gravatar.com 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 gravatar.com servers…

    Does anyone from the Gravatar.com team follow this? Please drop a line…
    Thanks!

  20. ciprianmp says:

    Ok, I worked it around and it works fine.
    I used a TalkPHPGravatar class posted and improved here: http://www.talkphp.com/script-giveaway/1905-gravatar-wrapper-class.html
    Now… what I got is:
    http://ciprianmp.com/plus – 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!

  21. Hieu Vo says:

    I appreciate your work a lots, ciprianmp :D

  22. sean says:

    Will it work?

  23. Ricky Hewitt says:

    Gravatar is already really simple to use using the PHP reference implementation (http://en.gravatar.com/site/implement/php) 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..?

  24. Laurie says:

    checking this out. very cool

an    joint
Follow

Get every new post delivered to your Inbox.

Join 9,123 other followers