by Kevin Schroeder | 12:00 am

Gravatar is a service that allows you to have a world-wide available avatar based off of your email address that can be used any place where you post comments or do anything really.  It is based at gravatar.com and it is free and easy to use.  Github uses it for their avatars.  And now, so does e_schrade.

The way it basically works is that you create an MD5 hash of the email address, so the spambots can't get it, and point that to the Gravatar website inside of an img src.  The code for building it is really quite easy.

class Zend_View_Helper_Gravatar extends Zend_View_Helper_Abstract
{

    public function gravatar($email, $size = 75)
    {
        $default = 'http://' . $_SERVER['HTTP_HOST'] . '/images/gravatar.gif';
        return "http://www.gravatar.com/avatar.php?gravatar_id="
               . md5( strtolower( $email ) )
               . "&default="
               . urlencode( $default )
               . "&size="
               . $size;
    }
    
}

Then in your view you simply reference the new helper.

<img src="<?php echo $this->gravatar($comment->getEmail()); ?>" style="margin: 5px;"/>

That's it.  Nice and simple.  Now go get yourself a Gravatar.

Tags: ,

Comments

No comments yet...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.