Clagnut

Menu

Gravatars in PHP

§ Blogging

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.