-ridya
Last Updated: February 25, 2016
·
1.047K
· euantor
Irnhmv6

Easily generate Gravatar images via PHP

Gravatars are the future. Pretty much everybody on the 'net is already using one and if they aren't likely will be in the next few years. They're also amazingly easy to generate - especially with a wrapper class - meaning you can start using them in your web applications with extreme ease.

In this protip I'll be showing you how to generate Gravatars using my SimpleGravatar class.

require 'SimpleGravatar.class.php';
$gravatar = new SimpleGravatar;

echo $gravatar->SetExtension('jpg')
         ->SetSize(64)
         ->SetDefault('mm')
         ->SetSecure()
         ->GetGravatar(true, 'euantor@mybb.com');

In the above, we are echoing out a Gravatar image with dimensions of 64px x 64px and a file extension of .jpg. If the email has no Gravatar associated with it we default to the "Mystery Man" Gravatar. We also specify that we wish to use the secure URL.

There are a few other options available within the class but I've tried to keep things as basic as possible. You can check the PHPDoc comments within the main class for more information.

Hope it's useful to somebody at some point!

Say Thanks
Respond