From e9117c5dc216693afd083a61cedad060d8a3944f Mon Sep 17 00:00:00 2001 From: damianb Date: Fri, 7 Oct 2011 15:58:09 -0500 Subject: [PATCH] New image default set and handle forcing default avatars The new image default set is "retro", recently added by the gravatar team. It generates a unique 8-bit pixel art image based on the email hash. To force default avatars, just specify an empty string for the email parameter of get/buildGravatarURL --- Gravatar.php | 19 +++++++++++++++---- README.markdown | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Gravatar.php b/Gravatar.php index a98b7ae..3fe798c 100644 --- a/Gravatar.php +++ b/Gravatar.php @@ -134,7 +134,7 @@ public function setDefaultImage($image) // Check $image against recognized gravatar "defaults", and if it doesn't match any of those we need to see if it is a valid URL. $_image = strtolower($image); - $valid_defaults = array('404' => 1, 'mm' => 1, 'identicon' => 1, 'monsterid' => 1, 'wavatar' => 1); + $valid_defaults = array('404' => 1, 'mm' => 1, 'identicon' => 1, 'monsterid' => 1, 'wavatar' => 1, 'retro' => 1); if(!isset($valid_defaults[$_image])) { if(!filter_var($image, FILTER_VALIDATE_URL)) @@ -237,14 +237,18 @@ public function buildGravatarURL($email, $hash_email = true) } // Tack the email hash onto the end. - if($hash_email == true) + if($hash_email == true && !empty($email)) { $url .= $this->getEmailHash($email); } - else + elseif(!empty($email)) { $url .= $email; } + else + { + $url .= str_repeat('0', 32); + } // Check to see if the param_cache property has been populated yet if($this->param_cache === NULL) @@ -262,8 +266,15 @@ public function buildGravatarURL($email, $hash_email = true) $this->params_cache = (!empty($params)) ? '?' . implode('&', $params) : ''; } + // Handle "null" gravatar requests. + $tail = ''; + if(empty($email)) + { + $tail = !empty($this->params_cache) ? '&f=y' : '?f=y'; + } + // And we're done. - return $url . $this->params_cache; + return $url . $this->params_cache . $tail; } /** diff --git a/README.markdown b/README.markdown index 1313050..1f37170 100644 --- a/README.markdown +++ b/README.markdown @@ -36,7 +36,7 @@ We'll assume you're using this git repository as a git submodule, and have it lo ### setting the default image Gravatar provides several pre-fabricated default images for use when the email address provided does not have a gravatar or when the gravatar specified exceeds your maximum allowed content rating. -The provided images are 'mm', 'identicon', 'monsterid', and 'wavatar'. To set the default iamge to use on your site, use the method `\emberlabs\GravatarLib\Gravatar->setDefaultImage()` +The provided images are 'mm', 'identicon', 'monsterid', 'retro', and 'wavatar'. To set the default iamge to use on your site, use the method `\emberlabs\GravatarLib\Gravatar->setDefaultImage()` In addition, you can also set your own default image to be used by providing a valid URL to the image you wish to use. Here are a couple of examples...