-
-
Save richsage/e214550d3d6673b86ff7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config.yml | |
services: | |
acme.twig.gravatar_extension: | |
class: Ray\CoreBundle\Twig\GravatarExtension | |
tags: | |
- { name: twig.extension } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Acme\DemoBundle\Twig\GravatarExtension | |
namespace Acme\DemoBundle\Twig; | |
class GravatarExtension extends \Twig_Extension | |
{ | |
private $secure_request = false; | |
public function getFilters() | |
{ | |
return array( | |
'gravatar' => new \Twig_Filter_Method($this, 'gravatarFilter'), | |
'sgravatar' => new \Twig_Filter_Method($this, 'secureGravatarFilter'), | |
); | |
} | |
public function gravatarFilter($email, $size = null, $default = null) | |
{ | |
$defaults = array( | |
'404', | |
'mm', | |
'identicon', | |
'monsterid', | |
'wavatar', | |
'retro', | |
'blank' | |
); | |
$hash = md5($email); | |
$url = $this->secure_request ? 'https://' : 'http://'; | |
$url .= 'www.gravatar.com/avatar/'.$hash; | |
// Size | |
if (!is_null($size)){ | |
$url .= "?s=$size"; | |
} | |
// Default | |
if (!is_null($default)){ | |
$url .= is_null($size) ? '?' : '&'; | |
$url .= 'd=' . (in_array($default, $defaults) ? $default : urlencode($default)); | |
} | |
return $url; | |
} | |
public function secureGravatarFilter($email, $size = null, $default = null) | |
{ | |
$this->secure_request = true; | |
} | |
public function getName() | |
{ | |
return 'gravatar_extension'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ email | gravatar }} # Will return default gravatar url | |
{{ email | gravatar(64) }} # Will return gravatar url 64x64 | |
{{ email | gravatar(200,'identicon') }} # Will return gravatar url 200x200 with default identicon | |
{{ email | sgravatar }} # Will return default secure (https) gravatar url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment