Published in Brighton, UK

Clagnut

Gravatars in PHP

The great thing about publishing a blog through your own CMS is that said CMS can be hacked around and added to at will. Of course the downside is that you have to write the code yourself. Unless someone else decides to publish some useful code for the taking. You’ll find below a snippet of code shows how I’ve implemented gravatars in PHP. How useful you’ll find it is open to question.

Essentially the code displays an image only if a gravatar is available. I didn’t want to display a default image so I check that the returned gravatar has a width greater than 1. If the commenter has also supplied a URL then the gravatar is made to link to it.

 = "";
if () {
     = "http://www.gravatar.com/avatar.php?
     gravatar_id=".md5().
     "&size=40&rating=PG";
     = @getimagesize();
     = ;
    if ( > 1) {
         = "<img src='' alt='".
         .
         "&#8217;s Gravatar' title='"..
         "&#8217;s Gravatar' class='gravatar' />";
        if () {
             = "<a href='".."'>".
             ."</a>";
        } 
    }
}

It’s pretty straight forward so I’ve left it uncommented. If you need anything explained, just ask.

19 November 2004

§ Blogging

16 comments

Next

Previous

Related posts

Keywords

Machine tags

Comments

  1. 1

    Why do you have to check the image at all?

    If you don’t send a default image URL to the Gravatar server, then you don’t get a default image. All you’ll get back is the URL to a 1×1 pixel transparent gif.

    Jonathan M. Hollin
    Jonathan M. Hollin’s Gravatar
    19 Nov 2004
    10:51 GMT
  2. 2

    True, Jonathan, but I don’t want to display the single pixel gif either (cos it will get a link and border around it).

    Rich
    Rich’s Gravatar
    19 Nov 2004
    10:57 GMT
  3. 3

    Oh I see.

    Just to clarify – this is because you are using a custom border. If you use Gravatar’s border system the 1×1 pixel is displayed without a border. This is how I have it implemented on my website.

    Jonathan M. Hollin
    Jonathan M. Hollin’s Gravatar
    19 Nov 2004
    11:01 GMT
  4. 4

    Psssst, Richard, before anybody notices: you’ve declared a variable that doesn’t exist:

    if () {
    = “<a href=’”..”’>”.Rich’s Gravatar.”</a>”;
    }

    I think instead of “Rich’s Gravatar” between the link tag, you mean “” ?

    Neat code, though, I suppose if you wanted to display your own default image, rather the Gravatar default, you could add an “else” condition after the “if ( > 1)” block, and insert your own image.

    Louise
    19 Nov 2004
    11:20 GMT
  5. 5

    I don’t think you need to use the HTML entity for the ampersand when fetching the image size via getimagesize(). Later on when you output it, sure.

    Justin
    19 Nov 2004
    14:09 GMT
  6. 6

    Louise – thanks for that. Duly corrected. That’s what happens when you try to tiny up your code for public consumption!

    Justin – yeah you’re right, but it seems to work OK so I’ll let it be (no doubt leaving problems for myself somewhere down the line).

    Rich
    Rich’s Gravatar
    19 Nov 2004
    15:07 GMT
  7. 7

    Doesn’t this reduce page loading times? I mean, on post with more than two comments?

    It sure took a while to load this page – but that might just be my bad.

    Mathias Bynens
    Mathias Bynens’s Gravatar
    19 Nov 2004
    16:35 GMT
  8. 8

    Copycat!

    ;o)

    Although yours is much better explained than mine.

    Matthew Pennell
    Matthew Pennell’s Gravatar
    19 Nov 2004
    19:18 GMT
  9. 9

    What about spoofing?

    If you know someone’s email what’s to stop you pretending to be them? Or am I missing something?

    Goynang
    20 Nov 2004
    10:48 GMT
  10. 10

    Testing to see if my Gravatar works here.

    Chu Yeow
    Chu Yeow’s Gravatar
    20 Nov 2004
    15:03 GMT
  11. 11

    Hmmm, it’s now a problem with my Gravatar account… OK so that means my WordPress nightly is broken somewhere. Anyway, thanks for providing the boilerplate code Rich!

    Chu Yeow
    Chu Yeow’s Gravatar
    20 Nov 2004
    15:12 GMT
  12. 12

    Goynang: yeah, and why not post comments under other people’s names? We all know it ain’t hard to pretend to be someone else on the web – Gravatar-wise or not…

    Mathias Bynens
    Mathias Bynens’s Gravatar
    20 Nov 2004
    15:15 GMT
  13. 13

    how do I make my gravatar show up?

    Hunter Chorey
    Hunter Chorey’s Gravatar
    20 Nov 2004
    21:39 GMT
  14. 14

    Just testing. :/

    Stewart Johnson
    Stewart Johnson’s Gravatar
    2 Dec 2004
    04:09 GMT
  15. 15

    Using this script increases page loading times a lot, especially on pages with lots of comments. Of course it does – for a post with 30 replies, for instance, the script would check 30 off-site images! All this takes a while.

    Mathias Bynens
    Mathias Bynens’s Gravatar
    23 Feb 2005
    14:34 GMT
  16. 16

    Mathias – It’s true this script will increase page loading times for the reasons you state, however Gravatars per se wouldn’t necessarily increase page rendering time.

    The script I presented could (and probably should) be optimised so it only checks for a Gravatar every now and then, not for each comment each time the page is displayed.

    Rich
    Rich’s Gravatar
    23 Feb 2005
    15:16 GMT

Add your comment

Comments are now closed on this post. If you have more to say please contact me directly.

Outside interest

Top Referrers