1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2015 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei;
10:
11: /**
12: * A service that generates a Gravatar URL for a specified email address
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: * @link https://gravatar.com/site/implement/images/
16: */
17: class Gravatar extends Base
18: {
19: /**
20: * The default image type or URL when email do not have a Gravatar image
21: *
22: * The image type could be 404, mm, identicon, monsterid or wavatar
23: *
24: * @var string
25: */
26: protected $default = 'mm';
27:
28: /**
29: * Whether display Gravatar in HTTPS request
30: *
31: * @var bool
32: */
33: protected $secure = false;
34:
35: /**
36: * The default size of image, from 1px up to 2048px
37: *
38: * @var int
39: */
40: protected $size = 80;
41:
42: /**
43: * The image size for `small` method
44: *
45: * @var int
46: */
47: protected $smallSize = 48;
48:
49: /**
50: * The image size for large
51: *
52: * @var int
53: */
54: protected $largeSize = 200;
55:
56: /**
57: * Generate a Gravatar URL for a specified email address
58: *
59: * @param string $email The email address
60: * @param int $size The image size in pixels
61: * @param string $default The image type or URL when email do not have a Gravatar image
62: * @param string $rating The image rating
63: * @return string A image URL
64: * @link http://gravatar.com/site/implement/images/php/
65: */
66: public function __invoke($email, $size = null, $default = null, $rating = null)
67: {
68: if ($this->secure) {
69: $url = 'https://secure.gravatar.com/avatar/';
70: } else {
71: $url = 'http://www.gravatar.com/avatar/';
72: }
73:
74: $url .= md5(strtolower(trim($email)));
75: $url .= '?s=' . ($size ?: $this->size);
76: $url .= '&d=' . ($default ?: $this->default);
77:
78: if ($rating) {
79: $url .= '&r=' . $rating;
80: }
81:
82: return $url;
83: }
84:
85: /**
86: * Generate a large size Gravatar URL for a specified email address
87: *
88: * @param string $email The email address
89: * @param string $default The image type or URL when email do not have a Gravatar image
90: * @param string $rating The image rating
91: * @return string
92: */
93: public function large($email, $default = null, $rating = null)
94: {
95: return $this->__invoke($email, $this->largeSize, $default, $rating);
96: }
97:
98: /**
99: * Generate a small size Gravatar URL for a specified email address
100: *
101: * @param string $email The email address
102: * @param string $default The image type or URL when email do not have a Gravatar image
103: * @param string $rating The image rating
104: * @return string
105: */
106: public function small($email, $default = null, $rating = null)
107: {
108: return $this->__invoke($email, $this->smallSize, $default, $rating);
109: }
110:
111: /**
112: * Sets the default image type or URL
113: *
114: * @param string $default
115: * @return $this
116: */
117: public function setDefault($default)
118: {
119: $this->default = urlencode($default);
120: return $this;
121: }
122: }
123: