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