├── .phpunit.result.cache ├── .styleci.yml ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Avatar.php ├── AvatarFormat.php ├── AvatarServiceProvider.php ├── AvatarSize.php ├── AvatarTheme.php ├── Contracts ├── AvatarFormatInterface.php ├── AvatarInterface.php ├── AvatarSizeInterface.php ├── AvatarThemeInterface.php └── ManagerInterface.php └── Facades └── Avatar.php /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | C:37:"PHPUnit\Runner\DefaultTestResultCache":107:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:1:{s:47:"Darbaoui\Avatar\Tests\AvatarTest::testBasicTest";d:0.137;}}} -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) darbaoui imad 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel ui avatar generator based on https://ui-avatars.com 2 | 3 | StyleCI Shield 4 | build:passed 5 | Total Downloads 6 | Latest Stable Version 7 | License 8 | 9 | ## Installation 10 | 11 | This package requires Laravel 5.4 or higher. 12 | 13 | 1. You can install the package via composer: 14 | 15 | ```bash 16 | composer require darbaoui/laravel-ui-avatars 17 | ``` 18 | 19 | ## Usage 20 | 21 | ```php 22 | 23 | use Darbaoui\Avatar\Facades\Avatar; 24 | 25 | // simple avatar 26 | Avatar::name('imad darbaoui') 27 | ->background('ffd1bf') 28 | ->color('ff4500')->get(); 29 | // avatar = https://ui-avatars.com/api/?name=imad+darbaoui&background=ffd1bf&color=ff4500 30 | 31 | 32 | // you can chain any other method if you want to add other parameters to your avatar 33 | Avatar::name('imad darbaoui') 34 | ->size(124) 35 | ->rounded() 36 | ->uppercase(false) 37 | ->svg() 38 | ->background('ffd1bf') 39 | ->color('ff4500')->get(); 40 | // avatar = https://ui-avatars.com/api/?size=124&rounded=1&uppercase=0&format=svg&name=imad+darbaoui&background=ffd1bf&color=ff4500 41 | ``` 42 | 43 | ## Available methods 44 | 45 | | methods | description | default value | 46 | | -------------- | --------------------------------------------------------------------------------- | ------------- | 47 | | `name()` | The name used to generate initials | `John Doe` | 48 | | `size()` | Avatar image size in pixels. Between: 16 and 512 | `64` | 49 | | `rounded()` | Boolean specifying if the returned image should be a circle | `false` | 50 | | `uppercase()` | Decide if the API should uppercase the name/initials. | `true` | 51 | | `fontSize()` | Font size in percentage of size. Between 0.1 and 1. | `0.5` | 52 | | `background()` | Hex color for the image background, without the hash (#) | `f0e9e9` | 53 | | `color()` | Hex color for the font, without the hash (#). | `8b5d5d` | 54 | | `length()` | Length of the generated initials. | `2` | 55 | | `bold()` | Boolean specifying if the returned letters should use a bold font. Default: false | `false` | 56 | | `svg()` | Decide to the avatar should be return SVG | `-` | 57 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "darbaoui/laravel-ui-avatars", 3 | "description": "generate an avatar image based on the name, size and background color and more", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "darbaoui imad", 8 | "email": "darbaoui@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=7.2" 13 | }, 14 | "require-dev": { 15 | "orchestra/testbench": "~3.8.0 || ^4.0 || ^5.0 || ^6.0", 16 | "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Darbaoui\\Avatar\\": "src" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "Darbaoui\\Avatar\\Tests\\": "tests" 26 | } 27 | }, 28 | "scripts": { 29 | "test": "vendor/bin/phpunit", 30 | "test-f": "vendor/bin/phpunit --filter", 31 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 32 | }, 33 | "config": { 34 | "sort-packages": true 35 | }, 36 | "extra": { 37 | "laravel": { 38 | "providers": [ 39 | "Darbaoui\\Avatar\\AvatarServiceProvider" 40 | ], 41 | "aliases": { 42 | "Avatar": "Darbaoui\\Avatar\\Facades\\Avatar" 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Avatar.php: -------------------------------------------------------------------------------- 1 | avatarSize = $avatarSize; 32 | $this->avatarFormat = $avatarFormat; 33 | $this->avatarTheme = $avatarTheme; 34 | } 35 | 36 | /** 37 | * Get the ui-avatar url. 38 | * 39 | * @return string 40 | */ 41 | public function get() 42 | { 43 | return $this->generateAvatarUrl(); 44 | } 45 | 46 | /** 47 | * Get the ui-avatar url. 48 | * 49 | * @return string 50 | */ 51 | public function __toString() 52 | { 53 | return $this->generateAvatarUrl(); 54 | } 55 | 56 | /** 57 | * Get the ui-avatar url. 58 | * 59 | * @return string 60 | */ 61 | public function generateAvatarUrl() 62 | { 63 | $parameters = array_merge( 64 | $this->avatarSize->getParameters(), 65 | $this->avatarFormat->getParameters(), 66 | $this->avatarTheme->getParameters() 67 | ); 68 | 69 | return self::UI_AVATAR_API.'?'.http_build_query($parameters); 70 | } 71 | 72 | /** 73 | * Define the name param used to generate the letters in the avatar. 74 | * 75 | * @param string $name 76 | * @return $this 77 | */ 78 | public function name(string $name) 79 | { 80 | $this->avatarFormat->set('name', $name); 81 | 82 | return $this; 83 | } 84 | 85 | /** 86 | * Define the avatar size param in the query it can between 64 and 512. 87 | * 88 | * @param int $size 89 | * @return $this 90 | */ 91 | public function size(int $size) 92 | { 93 | $this->avatarSize->set('size', $size); 94 | 95 | return $this; 96 | } 97 | 98 | /** 99 | * Define the font-size param in the query. 100 | * 101 | * @param float $fontSize 102 | * @return $this 103 | */ 104 | public function fontSize(float $fontSize) 105 | { 106 | $this->avatarSize->set('fontSize', $fontSize); 107 | 108 | return $this; 109 | } 110 | 111 | /** 112 | * Define the font color param in the query. 113 | * 114 | * @param string $background 115 | * @return $this 116 | */ 117 | public function color(string $color) 118 | { 119 | $this->avatarTheme->set('color', $color); 120 | 121 | return $this; 122 | } 123 | 124 | /** 125 | * Define the background color param in the query. 126 | * 127 | * @param string $background 128 | * @return $this 129 | */ 130 | public function background(string $background) 131 | { 132 | $this->avatarTheme->set('background', $background); 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * Define the length param in the query. 139 | * 140 | * @param int $length 141 | * @return $this 142 | */ 143 | public function length(int $length) 144 | { 145 | $this->avatarFormat->set('length', $length); 146 | 147 | return $this; 148 | } 149 | 150 | /** 151 | * Define the rounded param in the query. 152 | * 153 | * @return $this 154 | */ 155 | public function rounded() 156 | { 157 | $this->avatarFormat->set('rounded', true); 158 | 159 | return $this; 160 | } 161 | 162 | /** 163 | * Define the bold param in the query. 164 | * 165 | * @return $this 166 | */ 167 | public function bold() 168 | { 169 | $this->avatarFormat->set('bold', true); 170 | 171 | return $this; 172 | } 173 | 174 | /** 175 | * Define the uppercase param in the query. 176 | * 177 | * @param bool $uppercase 178 | * @return $this 179 | */ 180 | public function uppercase(bool $uppercase = true) 181 | { 182 | $this->avatarFormat->set('uppercase', $uppercase); 183 | 184 | return $this; 185 | } 186 | 187 | /** 188 | * Define the format param in the query. 189 | * 190 | * @return $this 191 | */ 192 | public function svg() 193 | { 194 | $this->avatarFormat->set('format', 'svg'); 195 | 196 | return $this; 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /src/AvatarFormat.php: -------------------------------------------------------------------------------- 1 | null, 16 | 'uppercase' => null, 17 | 'bold' => null, 18 | 'length' => 2, 19 | 'format' => 'png', 20 | ]; 21 | 22 | /** 23 | * Set the string value in argument as value of the key. 24 | * 25 | * @param string $key 26 | * @param mixed $value 27 | * @return void 28 | */ 29 | public function set($key, $value) 30 | { 31 | $this->parameters[$key] = $value; 32 | } 33 | 34 | /** 35 | * Get the argument value of the given key. 36 | * 37 | * @return mixed|null 38 | */ 39 | public function get($key) 40 | { 41 | return $this->parameters[$key] ? $this->parameters[$key] : null; 42 | } 43 | 44 | /** 45 | * Get the length parameter. 46 | * 47 | * @return mixed|null 48 | */ 49 | public function getLength() 50 | { 51 | return $this->parameters['length'] == 1 ? $this->parameters['length'] : null; 52 | } 53 | 54 | /** 55 | * Get the format parameter. 56 | * 57 | * @return mixed|null 58 | */ 59 | public function getSvg() 60 | { 61 | return $this->parameters['format'] === 'svg' ? $this->parameters['format'] : null; 62 | } 63 | 64 | /** 65 | * Get the format parameters. 66 | * 67 | * @return array 68 | */ 69 | public function getParameters(): array 70 | { 71 | return array_merge($this->parameters, [ 72 | 'length' => $this->getLength(), 73 | ]); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/AvatarServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('avatar', function () { 22 | return new Avatar( 23 | new AvatarSize(), 24 | new AvatarFormat(), 25 | new AvatarTheme() 26 | ); 27 | }); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/AvatarSize.php: -------------------------------------------------------------------------------- 1 | null, 20 | 'fontSize' => null, 21 | ]; 22 | 23 | /** 24 | * Set the string value in argument as value of the key. 25 | * 26 | * @param string $key 27 | * @param mixed $value 28 | * @return void 29 | */ 30 | public function set($key, $value) 31 | { 32 | $this->parameters[$key] = $value; 33 | } 34 | 35 | /** 36 | * Get the size parameter. 37 | * 38 | * @return mixed|null 39 | */ 40 | public function getSize() 41 | { 42 | return ($this->parameters['size'] >= self::AVATAR_MIN_SIZE && $this->parameters['size'] <= self::AVATAR_MAX_SIZE) ? $this->parameters['size'] : null; 43 | } 44 | 45 | /** 46 | * Get the font-size parameter. 47 | * 48 | * @return mixed|null 49 | */ 50 | public function getFontSize() 51 | { 52 | $intFontSize = (int) $this->parameters['fontSize'] * 10; 53 | 54 | return ($intFontSize >= 1 && $intFontSize <= 10) ? $this->parameters['fontSize'] : null; 55 | } 56 | 57 | /** 58 | * Get the size parameters. 59 | * 60 | * @return array 61 | */ 62 | public function getParameters(): array 63 | { 64 | return [ 65 | 'size' => $this->getSize(), 66 | 'font-size' => $this->getFontSize(), 67 | ]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/AvatarTheme.php: -------------------------------------------------------------------------------- 1 | null, 16 | 'color' => null, 17 | ]; 18 | 19 | /** 20 | * Set the string value in argument as value of the key. 21 | * 22 | * @param string $key 23 | * @param mixed $value 24 | * @return void 25 | */ 26 | public function set($key, $value) 27 | { 28 | $this->parameters[$key] = $value; 29 | } 30 | 31 | /** 32 | * Get the argument value of the given key. 33 | * 34 | * @return mixed|null 35 | */ 36 | public function get($key) 37 | { 38 | return $this->parameters[$key] ? $this->parameters[$key] : null; 39 | } 40 | 41 | /** 42 | * Get the theme parameters. 43 | * 44 | * @return array 45 | */ 46 | public function getParameters(): array 47 | { 48 | return $this->parameters; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Contracts/AvatarFormatInterface.php: -------------------------------------------------------------------------------- 1 |