├── .gitignore ├── Gravatar.php ├── LICENSE ├── README.md └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /Gravatar.php: -------------------------------------------------------------------------------- 1 | 'mail@cebe.cc', 17 | * 'size' => 128, 18 | * 'defaultImage' => 'monsterid', 19 | * // 'secure' => false, // will be autodetected 20 | * 'rating' => 'r', 21 | * 'options'=>[ 22 | * 'alt'=>'Gravatar image', 23 | * 'title'=>'Gravatar image', 24 | * ] 25 | * ]); 26 | * 27 | * 28 | * @author Carsten Brandt 29 | */ 30 | class Gravatar extends Widget 31 | { 32 | public $gravatarUrl = 'http://www.gravatar.com/avatar/'; 33 | public $gravatarUrlSecure = 'https://secure.gravatar.com/avatar/'; 34 | 35 | /** 36 | * @var boolean whether to use [[gravatarUrl]] or [[gravatarUrlSecure]] as base url. 37 | * If not set it will be detected by current request. 38 | */ 39 | public $secure; 40 | 41 | /** 42 | * @var int image size in pixel 43 | * @link http://en.gravatar.com/site/implement/images/#size 44 | */ 45 | public $size = 128; 46 | 47 | /** 48 | * @var string the email to use 49 | */ 50 | public $email; 51 | 52 | /** 53 | * Can be one of '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro' or a url to default image. 54 | * @var string default image to use if no gravatar available 55 | * @link http://en.gravatar.com/site/implement/images/#default-image 56 | */ 57 | public $defaultImage; 58 | 59 | /** 60 | * Can be one of 'g', 'pg', 'r', 'x' 61 | * @var string gravatar image rating 62 | * @link http://en.gravatar.com/site/implement/images/#rating 63 | */ 64 | public $rating = 'g'; 65 | 66 | /** 67 | * @var string can be 'png' or 'jpg' 68 | * @link http://en.gravatar.com/site/implement/images/#base-request 69 | */ 70 | public $fileType; 71 | 72 | /** 73 | * @var array html options for the image tag 74 | */ 75 | public $options = []; 76 | 77 | 78 | public function run() 79 | { 80 | if (!isset($this->options['alt'])) { 81 | $this->options['alt'] = 'Gravatar image'; 82 | } 83 | echo Html::img($this->getImageUrl(), $this->options); 84 | } 85 | 86 | /** 87 | * @return string generates the gravatar image url 88 | */ 89 | public function getImageUrl() 90 | { 91 | if ($this->secure === null) { 92 | $this->secure = \Yii::$app->request->isSecureConnection; 93 | } 94 | $url = $this->secure ? $this->gravatarUrlSecure : $this->gravatarUrl; 95 | $url .= $this->getEmailHash() . (($this->fileType !== null) ? '.' . $this->fileType : ''); 96 | 97 | $params = [ 98 | 'r' => $this->rating, 99 | 's' => $this->size, 100 | ]; 101 | if ($this->defaultImage !== null) { 102 | $params['d'] = $this->defaultImage; 103 | } 104 | 105 | $url .= '?' . http_build_query($params); 106 | return $url; 107 | } 108 | 109 | private $_emailHash; 110 | 111 | /** 112 | * Generates email hash for gravatar url 113 | * 114 | * @link http://en.gravatar.com/site/implement/hash/ 115 | * @return string md5 hash of the trimmed lowercase [[email]] 116 | * @throws \yii\base\InvalidConfigException if no email has been specified. 117 | */ 118 | public function getEmailHash() 119 | { 120 | if ($this->_emailHash !== null) { 121 | return $this->_emailHash; 122 | } elseif ($this->email === null) { 123 | throw new InvalidConfigException('No email specified for Gravatar image Widget.'); 124 | } 125 | return $this->_emailHash = md5(strtolower(trim($this->email))); 126 | } 127 | 128 | /** 129 | * Sets the email hash to use for gravatar url 130 | * @param $hash 131 | */ 132 | public function setEmailHash($hash) 133 | { 134 | $this->_emailHash = $hash; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Carsten Brandt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | yii2-gravatar 2 | ============= 3 | 4 | Gravatar Widget for Yii Framework 2 5 | 6 | How to install? 7 | --------------- 8 | 9 | Get it via [composer](http://getcomposer.org/) by adding the package to your `composer.json`: 10 | 11 | ```json 12 | { 13 | "require": { 14 | "cebe/yii2-gravatar": "^1.0" 15 | } 16 | } 17 | ``` 18 | 19 | Alternatively just run `composer require cebe/yii2-gravatar`. 20 | 21 | You may also check the package information on [packagist](https://packagist.org/packages/cebe/yii2-gravatar) 22 | and follow the [extension page on yiiframework.com](https://www.yiiframework.com/extension/cebe/yii2-gravatar) for udpates. 23 | 24 | Usage 25 | ----- 26 | 27 | ```php 28 | 'mail@cebe.cc', 30 | 'options' => [ 31 | 'alt' => 'Carsten Brandt' 32 | ], 33 | 'size' => 32 34 | ]) ?> 35 | ``` 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cebe/yii2-gravatar", 3 | "description": "Gravatar Widget for Yii 2", 4 | "keywords": ["yii", "gravatar"], 5 | "type": "yii2-extension", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Carsten Brandt", 10 | "email": "mail@cebe.cc" 11 | } 12 | ], 13 | "support": { 14 | "issues": "https://github.com/cebe/yii2-gravatar/issues", 15 | "irc": "irc://irc.freenode.net/yii", 16 | "source": "https://github.com/cebe/yii2-gravatar" 17 | }, 18 | "require": { 19 | "yiisoft/yii2": "*" 20 | }, 21 | "autoload": { 22 | "psr-0": { "cebe\\gravatar\\": "" } 23 | }, 24 | "target-dir": "cebe/gravatar" 25 | } 26 | 27 | --------------------------------------------------------------------------------