PHP Gravatar Class

I was recently working on a small side project when I ran across a need for a php implementation of a class that would produce Gravatar urls.

Generating Gravatar urls is not a complicated affair. In fact, it’s not out of the question to write a one-off gravatar function as needed. I wanted something slightly more robust and reuseable. I was surprised as I surveyed the php gravatar classes in github to find a morass of over engineered solutions that were often tightly coupled to  a particular framework.

It’s understandable that the framework specific examples would be popular on github. More often than not a php coder will be working inside a standard framework and is more likely to seek code designed to plug-in directly to their framework of choice. Parsing the program flow and logic of these modules is difficult if you’re not familiar with the conventions and peculiarities of the framework for which they are designed. I was looking for a more barebones implementation that I can drop in a variety of contexts or use as a skeleton when implementing a gravatar class in a specific context.

After a page or two of browsing I decided it would be faster to just write one myself. It is a very simple and straightforward gravatar class. It uses sensible defaults and all parameters except the email address are optional.

Usage is as a simple as:

$gravatar_url = gravatar::url('');

If you are feeling more specific, pass parameters like so:

$email = filter_input(INPUT_GET,'e',FILTER_SANITIZE_EMAIL);
    $params = array(
        'secure'     => false,
        'default'    => 'monsterid',
        'rating'     => 'x',
        'size'       => 256
    $url = gravatar::url($email,$params);
    echo '<h1>'. $url . '</h1> <img src="'. $url .'" alt="" / >';

To clone or copy my simple php gravatar class:

Lance Cameron Kidwell

6 December 2011

Posted in Coding

In lieu of comments, please talk about this post with smart people at Hacker News Hacker News or, if you're the twittering sort, click this handsome button:

Prove Me Wrong: