Simple PHP Pavatar Method

Microsoft Passport was a good idea. OpenID is a better idea. Gravatar is a good idea. Pavatar is a better idea. I think whenever a system can be open and decentralized, it should be (see my upcoming post on decentralized Twitter). For those of you who like the idea and want to use it, here’s a quick and dirty PHP method to get the Pavatar image for a given URL (supports all three Pavatar techniques)…

/**
 * Determines a URL's associated Pavatar (http://pavatar.com/)
 * Returns a URL on success, FALSE on failure
 * Does NOT verify that the URL is a valid image
 *
 * @author Chris Morrell (http://cmorrell.com)
 * @param string $url
 * @return mixed
 */
function getPavatar($url)
{
    // Grab Data
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $page = curl_exec($curl);
    curl_close($curl);

    // On Fail
    if (!$page)
    {
        return false;
    }

    // X-Pavatar Header Method
    elseif (preg_match('/X-Pavatar:\s*(.*?)(\s|\n)/i', $page, $matches))
    {
        return $matches[1];
    }

    // <link> Method
    elseif (preg_match('/<link.*?rel="pavatar".*?>/i', $page, $matches))
    {
        preg_match('/href="(.*?)"/i', $matches[0], $matches);
        return $matches[1];
    }

    // Default URL Method
    else
    {
        $info = parse_url($url);
        return "{$info['scheme']}://{$info['host']}/pavatar.png";
    }
}

Usage:

$pavatar = getPavatar('http://cmorrell.com');

Enjoy!

Related posts:

  1. 2 Simple Twitter UI Tweaks Update: it was bugging me, so I went ahead and...
  2. E-Mail Address Obfuscator Class A project I’m working on needs to output text that...
This entry was posted in Misc. Bookmark the permalink.
  • http://jeenaparadies.net Jeena Paradies

    Hi, your “Default URL Method” is not quite right, It does not look at http://example.com/myname/pavatar.png, but that’s not such a big deal, I was thinking about removing this method anyway.

  • b_dubb

    is this similar to the concept of ‘federation’ in Google Wave?  i agree about decentralizing