Skip to content

Instantly share code, notes, and snippets.

@sisodiakaran
Created October 1, 2013 05:46
Show Gist options
  • Save sisodiakaran/6774343 to your computer and use it in GitHub Desktop.
Save sisodiakaran/6774343 to your computer and use it in GitHub Desktop.
CakePHP-GravatarHelper
<?php
class GravatarHelper extends AppHelper {
/**
* Get either a Gravatar URL or complete image tag for a specified email address.
*
* @param string $email The email address
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
* @param boole $img True to return a complete IMG tag False for just the URL
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
* @return String containing either just a URL or a complete image tag
* @source http://gravatar.com/site/implement/images/php/
*/
public function image( $email, $s = 80, $img = false, $atts = array() ) {
$d = 'mm'; //Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
$r = 'g'; //Maximum rating (inclusive) [ g | pg | r | x ]
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
}
@sisodiakaran
Copy link
Author

Usage

Just put GravatarHelper.php file in your app/View/Helper folder, and add below code in your view.
echo $this->Gravatar->image($email = 'foo@anydomain.com', $size=200, $image_tag = TRUE, array('alt' => '...', 'title' => '...', 'class' => 'some-image-class' ));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment