├── .gitignore ├── LICENSE ├── composer.json ├── config └── gravatar.php ├── readme.md └── src ├── Exceptions └── InvalidEmailException.php ├── Facades └── Gravatar.php ├── Gravatar.php └── GravatarServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Creativeorange B.V. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "creativeorange/gravatar", 3 | "description": "A Laravel Gravatar package for retrieving gravatar image URLs or checking the existance of an image.", 4 | "keywords": [ 5 | "laravel", 6 | "gravatar", 7 | "avatar" 8 | ], 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Jaco Tijssen", 13 | "email": "jaco@creativeorange.nl", 14 | "homepage": "https://www.creativeorange.nl", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.4.0", 20 | "illuminate/support": "^5|^6|^7|^8|^9|^10.0|^11.0|^12.0" 21 | }, 22 | "require-dev": { 23 | "php": ">=7.2", 24 | "nunomaduro/larastan": "^0.6.2|^2.4", 25 | "orchestra/testbench": "^5.4|^8.0|^9.0|^10.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Creativeorange\\Gravatar\\": "src/" 30 | } 31 | }, 32 | "extra": { 33 | "laravel": { 34 | "providers": [ 35 | "Creativeorange\\Gravatar\\GravatarServiceProvider" 36 | ], 37 | "aliases": { 38 | "Gravatar": "Creativeorange\\Gravatar\\Facades\\Gravatar" 39 | } 40 | } 41 | }, 42 | "minimum-stability": "stable", 43 | "scripts": { 44 | "analyze": "phpstan analyse -c vendor/nunomaduro/larastan/extension.neon -l 5 src/" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /config/gravatar.php: -------------------------------------------------------------------------------- 1 | [ 5 | 6 | // By default, images are presented at 80px by 80px if no size parameter is supplied. 7 | // You may request a specific image size, which will be dynamically delivered from Gravatar 8 | // by passing a single pixel dimension (since the images are square): 9 | 'size' => 80, 10 | 11 | // the fallback image, can be a string or a url 12 | // for more info, visit: https://docs.gravatar.com/api/avatars/images/#default-image 13 | 'fallback' => 'mp', 14 | 15 | // would you like to return a https://... image 16 | 'secure' => false, 17 | 18 | // Gravatar allows users to self-rate their images so that they can indicate if an image 19 | // is appropriate for a certain audience. By default, only 'G' rated images are displayed 20 | // unless you indicate that you would like to see higher ratings. 21 | // Available options: 22 | // g: suitable for display on all websites with any audience type. 23 | // pg: may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence. 24 | // r: may contain such things as harsh profanity, intense violence, nudity, or hard drug use. 25 | // x: may contain hardcore sexual imagery or extremely disturbing violence. 26 | 'maximumRating' => 'g', 27 | 28 | // If for some reason you wanted to force the default image to always load, you can do that setting this to true 29 | 'forceDefault' => false, 30 | 31 | // If you require a file-type extension (some places do) then you may also add an (optional) .jpg extension to that URL 32 | 'forceExtension' => 'jpg', 33 | ] 34 | ]; 35 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Gravatar for Laravel 5 - 12 2 | 3 | [![Total Downloads](https://poser.pugx.org/creativeorange/gravatar/d/total.svg)](https://packagist.org/packages/creativeorange/gravatar) 4 | [![Latest Stable Version](https://poser.pugx.org/creativeorange/gravatar/v/stable.svg)](https://packagist.org/packages/creativeorange/gravatar) 5 | [![License](https://poser.pugx.org/creativeorange/gravatar/license.svg)](https://packagist.org/packages/creativeorange/gravatar) 6 | 7 | ## Installation 8 | 9 | First, pull in the package through Composer via the command line: 10 | ```js 11 | composer require creativeorange/gravatar ~1.0 12 | ``` 13 | 14 | or add the following to your composer.json file and run `composer update`. 15 | 16 | ```js 17 | "require": { 18 | "creativeorange/gravatar": "~1.0" 19 | } 20 | ``` 21 | 22 | Then include the service provider within (Laravel 5.3 or below) `app/config/app.php`. 23 | 24 | ```php 25 | 'providers' => [ 26 | 'Creativeorange\Gravatar\GravatarServiceProvider' 27 | ]; 28 | ``` 29 | 30 | If using Laravel 5.4, include service provider withing `config/app.php` 31 | 32 | ```php 33 | 'providers' => [ 34 | Creativeorange\Gravatar\GravatarServiceProvider::class 35 | ]; 36 | ``` 37 | 38 | If you want to use the facade, add this to de bottom of `app/config/app.php` 39 | And, for convenience, add a facade alias to this same file at the bottom: 40 | 41 | ```php 42 | 'aliases' => [ 43 | 'Gravatar' => 'Creativeorange\Gravatar\Facades\Gravatar', 44 | ]; 45 | ``` 46 | 47 | If you are using Laravel 5.4 or greater, add as follows, add to `config/app.php` 48 | 49 | ```php 50 | 'aliases' => [ 51 | 'Gravatar' => Creativeorange\Gravatar\Facades\Gravatar::class, 52 | ]; 53 | ``` 54 | 55 | 56 | Finally, publish the config by running the `php artisan vendor:publish` command 57 | 58 | 59 | ## Usage 60 | 61 | Within your controllers or views, you can use 62 | 63 | ```php 64 | Gravatar::get('email@example.com'); 65 | ``` 66 | 67 | this will return the URL to the gravatar image of the specified email address. 68 | In case of a non-existing gravatar, it will return return a URL to a placeholder image. 69 | You can set the type of the placeholder in the configuration option `fallback`. 70 | For more information, visit [gravatar.com](https://docs.gravatar.com/api/avatars/images/#default-image) 71 | 72 | Alternatively, you can check for the existence of a gravatar image by using 73 | 74 | ```php 75 | Gravatar::exists('email@example.com'); 76 | ``` 77 | 78 | This will return a boolean (`true` or `false`). 79 | 80 | Or you can pass a url to a custom image using the fallback method: 81 | 82 | ```php 83 | Gravatar::fallback('http://urlto.example.com/avatar.jpg')->get('email@example.com'); 84 | ``` 85 | 86 | 87 | ## Configuration 88 | 89 | You can create different configuration groups to use within your application and pass the group name as a second parameter to the `get`-method: 90 | 91 | There is a default group in `config/gravatar.php` which will be used when you do not specify a second parameter. 92 | 93 | If you would like to add more groups, feel free to edit the `config/gravatar.php` file. For example: 94 | 95 | ```php 96 | return array( 97 | 'default' => array( 98 | 'size' => 80, 99 | 'fallback' => 'mm', 100 | 'secure' => false, 101 | 'maximumRating' => 'g', 102 | 'forceDefault' => false, 103 | 'forceExtension' => 'jpg', 104 | ), 105 | 'small-secure' => array ( 106 | 'size' => 30, 107 | 'secure' => true, 108 | ), 109 | 'medium' => array ( 110 | 'size' => 150, 111 | ) 112 | ); 113 | ``` 114 | 115 | then you can use the following syntax: 116 | 117 | ```php 118 | Gravatar::get('email@example.com', 'small-secure'); // will use the small-secure group 119 | Gravatar::get('email@example.com', 'medium'); // will use the medium group 120 | Gravatar::get('email@example.com', 'default'); // will use the default group 121 | Gravatar::get('email@example.com'); // will use the default group 122 | ``` 123 | 124 | Alternatively, you could also pass an array directly as the second parameter as inline options. So, instead of passing a configuration key, you pass an array, which will be merged with the default group: 125 | 126 | ```php 127 | Gravatar::get('email@example.com', ['size'=>200]); 128 | ``` 129 | 130 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidEmailException.php: -------------------------------------------------------------------------------- 1 | fallback = $fallback; 66 | } else { 67 | $this->fallback = false; 68 | } 69 | 70 | return $this; 71 | } 72 | 73 | /** 74 | * Check if Gravatar has an avatar for the given email address 75 | * 76 | * @param string $email 77 | * @return bool 78 | * @throws InvalidEmailException 79 | */ 80 | public function exists($email) 81 | { 82 | $this->checkEmail($email); 83 | $this->email = $email; 84 | 85 | $this->setConfig(['fallback' => 404]); 86 | 87 | $headers = @get_headers($this->buildUrl()); 88 | 89 | return (bool) ($headers) ? strpos($headers[0], '200') : false; 90 | } 91 | 92 | /** 93 | * Get the gravatar url 94 | * 95 | * @param string $email 96 | * @param string|array|null $configGroup 97 | * @return string 98 | * @throws InvalidEmailException 99 | */ 100 | public function get($email, $configGroup = 'default') 101 | { 102 | $this->checkEmail($email); 103 | 104 | $this->setConfig($configGroup); 105 | $this->email = $email; 106 | 107 | return $this->buildUrl(); 108 | } 109 | 110 | /** 111 | * Helper function for setting the config based on either: 112 | * 1. The name of a config group 113 | * 2. A custom array 114 | * 3. The default group in the config 115 | * 116 | * @param string|array|null $group 117 | * @return $this 118 | */ 119 | private function setConfig($group = null) 120 | { 121 | if ( 122 | is_string($group) 123 | && $group != 'default' 124 | ) { 125 | $this->config = Arr::dot(array_replace_recursive(config('gravatar.default'), config('gravatar.' . $group))); 126 | } elseif (is_array($group)) { 127 | $this->config = Arr::dot(array_replace_recursive(config('gravatar.default'), $group)); 128 | } else { 129 | $this->config = Arr::dot(config('gravatar.default')); 130 | } 131 | 132 | return $this; 133 | } 134 | 135 | /** 136 | * Helper function to retrieve config settings. 137 | * 138 | * @param string $value 139 | * @param mixed $default 140 | * @return mixed 141 | */ 142 | protected function c($value, $default = null) 143 | { 144 | return array_key_exists($value, $this->config) ? $this->config[$value] : $default; 145 | } 146 | 147 | /** 148 | * Helper function to sha256 hash the email address 149 | * 150 | * @return string 151 | */ 152 | private function hashEmail() 153 | { 154 | return hash('sha256', strtolower(trim($this->email))); 155 | } 156 | 157 | /** 158 | * @return string 159 | */ 160 | private function getExtension() 161 | { 162 | $v = $this->c('forceExtension'); 163 | 164 | return $v ? '.' . $v : ''; 165 | } 166 | 167 | /** 168 | * @return string 169 | */ 170 | private function buildUrl() 171 | { 172 | $url = $this->c('secure') === true ? $this->secureBaseUrl : $this->publicBaseUrl; 173 | $url .= $this->hashEmail(); 174 | $url .= $this->getExtension(); 175 | $url .= $this->getUrlParameters(); 176 | 177 | return $url; 178 | } 179 | 180 | /** 181 | * @return string 182 | */ 183 | private function getUrlParameters() 184 | { 185 | $build = array(); 186 | 187 | foreach (get_class_methods($this) as $method) { 188 | if (substr($method, -strlen('Parameter')) !== 'Parameter') { 189 | continue; 190 | } 191 | 192 | if ($called = call_user_func(array($this, $method))) { 193 | $build = array_replace($build, $called); 194 | } 195 | } 196 | 197 | return '?' . http_build_query($build); 198 | } 199 | 200 | /** 201 | * @return array|null 202 | */ 203 | private function sizeParameter() 204 | { 205 | if ( 206 | !$this->c('size') 207 | || !is_integer($this->c('size')) 208 | ) { 209 | return null; 210 | } 211 | 212 | return array('s' => $this->c('size')); 213 | } 214 | 215 | /** 216 | * @return array 217 | */ 218 | private function defaultParameter() 219 | { 220 | if ($this->fallback === false) { 221 | $this->fallback = $this->c('fallback') ? $this->c('fallback') : null; 222 | } 223 | 224 | return array('d' => $this->fallback); 225 | } 226 | 227 | /** 228 | * @return array|null 229 | */ 230 | private function ratingParameter() 231 | { 232 | $rating = $this->c('maximumRating'); 233 | 234 | if ( 235 | !$rating 236 | || !in_array($rating, array('g', 'pg', 'r', 'x')) 237 | ) { 238 | return null; 239 | } 240 | 241 | return array('r' => $rating); 242 | } 243 | 244 | /** 245 | * @return array|null 246 | */ 247 | private function forceDefaultParameter() 248 | { 249 | if ($this->c('forceDefault') === true) { 250 | return array('forcedefault' => 'y'); 251 | } 252 | 253 | return null; 254 | } 255 | 256 | /** 257 | * Check if the provided email address is valid 258 | * 259 | * @param string $email 260 | * @throws InvalidEmailException 261 | */ 262 | private function checkEmail($email) 263 | { 264 | $validator = Validator::make(['email' => $email], ['email' => 'required|email']); 265 | 266 | if ($validator->fails()) 267 | throw new InvalidEmailException('Please specify a valid email address'); 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /src/GravatarServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom($configPath, 'gravatar'); 23 | 24 | $this->app->singleton('gravatar', function () { 25 | return new Gravatar; 26 | }); 27 | } 28 | 29 | /** 30 | * Get the services provided by the provider. 31 | * 32 | * @return array 33 | */ 34 | public function provides() 35 | { 36 | return []; 37 | } 38 | 39 | 40 | public function boot () 41 | { 42 | $this->publishes([ 43 | __DIR__.'/../config/gravatar.php' => config_path('gravatar.php'), 44 | ]); 45 | } 46 | 47 | } 48 | --------------------------------------------------------------------------------