|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace modules\users\widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use yii\helpers\Html; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AvatarWidget |
|
11
|
|
|
* @package modules\users\widgets |
|
12
|
|
|
* |
|
13
|
|
|
* @property array $imageOptions |
|
14
|
|
|
* @property string $email |
|
15
|
|
|
* @property-read string $userEmail |
|
16
|
|
|
* @property string|int $size |
|
17
|
|
|
*/ |
|
18
|
|
|
class AvatarWidget extends Widget |
|
19
|
|
|
{ |
|
20
|
|
|
public $imageOptions = [ |
|
21
|
|
|
'class' => 'img-circle', |
|
22
|
|
|
]; |
|
23
|
|
|
public $email = ''; |
|
24
|
|
|
public $size = '80'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function init() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::init(); |
|
32
|
|
|
$this->email = !empty($this->email) ? $this->email : $this->getUserEmail(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function run() |
|
39
|
|
|
{ |
|
40
|
|
|
echo $this->getGravatar($this->email, $this->size, 'mm', 'g', true, $this->imageOptions); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get either a Gravatar URL or complete image tag for a specified email address. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $email The email address |
|
47
|
|
|
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
|
48
|
|
|
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
|
49
|
|
|
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
|
50
|
|
|
* @param bool $img True to return a complete IMG tag False for just the URL |
|
51
|
|
|
* @param array $attr Optional, additional key/value attributes to include in the IMG tag |
|
52
|
|
|
* @return string containing either just a URL or a complete image tag |
|
53
|
|
|
* @source https://gravatar.com/site/implement/images/php/ |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getGravatar($email = '', $s = '80', $d = 'mm', $r = 'g', $img = false, $attr = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$url = 'https://www.gravatar.com/avatar/'; |
|
58
|
|
|
$url .= md5(strtolower(trim($email))) . '?'; |
|
59
|
|
|
$url .= http_build_query([ |
|
60
|
|
|
's' => $s, |
|
61
|
|
|
'd' => $d, |
|
62
|
|
|
'r' => $r, |
|
63
|
|
|
]); |
|
64
|
|
|
return $img ? Html::img($url, $attr) : $url; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getUserEmail() |
|
71
|
|
|
{ |
|
72
|
|
|
/** @var object $user */ |
|
73
|
|
|
$user = Yii::$app->user->identity; |
|
74
|
|
|
return (!Yii::$app->user->isGuest) ? $user->email : $this->email; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|