├── .coveralls.yml ├── .travis.yml ├── LICENSE.md ├── README.md ├── composer.json ├── config └── ui-avatars.php ├── phpunit.xml └── src ├── Avatar.php ├── AvatarGeneratorFactory.php ├── AvatarGeneratorInterface.php ├── Generators ├── ApiGenerator.php └── LocalGenerator.php ├── HasAvatar.php └── UIAvatarsServiceProvider.php /.coveralls.yml: -------------------------------------------------------------------------------- 1 | coverage_clover: build/logs/clover.xml 2 | json_path: build/logs/coveralls-upload.json 3 | service_name: travis-ci -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.1 5 | - 7.2 6 | 7 | before_script: 8 | - mkdir -p build/logs 9 | - composer self-update 10 | - composer install --prefer-source --no-interaction --dev 11 | 12 | script: 13 | - vendor/bin/phpunit --coverage-clover build/logs/clover.xml 14 | 15 | after_script: 16 | - php vendor/bin/coveralls -v 17 | 18 | after_success: 19 | - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;' -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Rackbeat & Lasse Rafn 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Official Laravel wrapper for https://ui-avatars.com 2 | 3 | 4 |

5 | Build Status 6 | Coverage 7 | Total Downloads 8 | Latest Stable Version 9 | License 10 |

11 | 12 | ## Installation 13 | 14 | You just require using composer and you're good to go! 15 | 16 | ```bash 17 | composer require rackbeat/laravel-ui-avatars 18 | ``` 19 | 20 | The Service Provider is automatically registered. 21 | 22 | ## Setup 23 | 24 | To setup the config file, you publish it like so: 25 | 26 | ```bash 27 | php artisan vendor:publish --provider="Rackbeat\UIAvatars\UIAvatarsServiceProvider" 28 | ``` 29 | 30 | You can edit the file in `config/ui-avatars.php`. 31 | 32 | ## Usage 33 | 34 | ### 1. Add the `Rackbeat\UIAvatars\HasAvatar` trait to your Model (e.g. `App\Users`) 35 | 36 | ```php 37 | // ... 38 | class User extends Authenticatable { 39 | use \Rackbeat\UIAvatars\HasAvatar; 40 | // ... 41 | } 42 | ``` 43 | 44 | ### 2. Create a new method on your Model. 45 | 46 | This method is practically a proxy to call the `HasAvatar` methods. It will return a gravatar from the e-mail, with a fallback to the avatar using `ui-avatars.com` API. 47 | 48 | ```php 49 | public function getAvatar( $size = 64 ) { 50 | return $this->getGravatar( $this->email, $size ); 51 | } 52 | ``` 53 | 54 | NOTICE: Gravatar is only available using the API, not locally generated avatars. 55 | 56 | ### 3. (Optional) Re-define the name field 57 | 58 | Assuming you're not using the default `User` Model in Laravel, you can override which field is being used for the name. 59 | 60 | ```php 61 | public function getAvatarNameKey( ) { 62 | return 'full_name'; 63 | } 64 | ``` 65 | 66 | ## Available methods 67 | 68 | ### `getInitials($length=null)` 69 | 70 | Returns the generated initials, from the name, used in the avatar. 71 | 72 | Default `$length` can be defined in `config/ui-avatars.php` (`length` key) 73 | 74 | ### `getUrlfriendlyAvatar($size=null)` 75 | 76 | Return a urlfriendly formatted avatar (URL or base64). 77 | 78 | Example usage: 79 | ```html 80 | 81 | ``` 82 | 83 | Default `$size` can be defined in `config/ui-avatars.php` (`image_size` key) 84 | 85 | ### `getAvatarBase64($size=null)` 86 | 87 | Return a base64 representation of the avatar. 88 | 89 | Default `$size` can be defined in `config/ui-avatars.php` (`image_size` key) 90 | 91 | ### `getAvatarImage($size=null)` 92 | 93 | Return a Image object of the avatar. 94 | 95 | Default `$size` can be defined in `config/ui-avatars.php` (`image_size` key) 96 | 97 | ### `getGravatar($email, $size=null)` 98 | 99 | Return a link to a Gravatar image using the specified email, and a fallback to using our own generator (assuming provider = `api`) 100 | 101 | Default `$size` can be defined in `config/ui-avatars.php` (`image_size` key) 102 | 103 | ## Available options 104 | 105 | In the config file you can specify different options. 106 | 107 | | Key | Description | Default | Available Options / Type | 108 | | ------------- | ------------- | ------------- | ------------- | 109 | | provider | Which provider to use for generating avatars. | `api` | `local` or `api` | 110 | | length | Max initials length (amount of letters) | `2` | Number, min: 1, max: unlimited | 111 | | image_size | Default width & height for the generated avatar. | `48` | Number, min: 1, max: unlimited (512 using `api` provider) | 112 | | font_size | Font size in percentage of `image_size`. | `0.5` | Number, min: 0, max: 1 | 113 | | rounded | Should the generated avatar be a rounded circle? (its recommended to disable and round using CSS) | `false` | Boolean | 114 | | smooth_rounding | If `rounded` is enabled, and provider is `local`, you can enable smoother rounding with the cost of performance. | `true` | Boolean | 115 | | uppercase | Should the initials be forced uppercase or not. | `true` | Boolean | 116 | | background_color | Default HEX background color for avatars. | `#a0a0a0` | Hex | 117 | | font_color | Default HEX font color for avatars. | `#222` | Hex | 118 | | providers | List of available providers. For you to add your own provider. | | Array of providers | 119 | 120 | ## Requirements 121 | * PHP >= 7.1 122 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rackbeat/laravel-ui-avatars", 3 | "description": "Official Laravel wrapper around ui-avatars.com and LasseRafn/php-initial-avatar-generator", 4 | "keywords": [ 5 | "laravel", 6 | "ui-avatars", 7 | "php", 8 | "avatars", 9 | "initials" 10 | ], 11 | "type": "library", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Lasse Rafn", 16 | "email": "lasserafn@gmail.com" 17 | }, 18 | { 19 | "name": "Rackbeat", 20 | "email": "open-source@rackbeat.com" 21 | } 22 | ], 23 | "require": { 24 | "php": ">=7.1", 25 | "lasserafn/php-initial-avatar-generator": "^4.0", 26 | "illuminate/support": "^7.0|^8.0|^9.0|^10.0" 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit": "^7.0|^9.5.10", 30 | "satooshi/php-coveralls": "^1.0" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Rackbeat\\UIAvatars\\": "src/" 35 | } 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "UIAvatars\\Tests\\": "tests/" 40 | } 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Rackbeat\\UIAvatars\\UIAvatarsServiceProvider" 49 | ] 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /config/ui-avatars.php: -------------------------------------------------------------------------------- 1 | 'local', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Default region 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Select which region to use when using the "api" provider. 24 | | 25 | | Options: 26 | | - na = North America 27 | | - eu = Europe 28 | | - null = Server default (can vary) 29 | */ 30 | 'default_region' => 'na', 31 | 32 | /* 33 | |-------------------------------------------------------------------------- 34 | | Initials length 35 | |-------------------------------------------------------------------------- 36 | | 37 | | Set the amount of characters the initials can consist of. 38 | */ 39 | 'length' => 2, 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | Image size 44 | |-------------------------------------------------------------------------- 45 | | 46 | | Set the width and height of the image, in pixels. 47 | */ 48 | 'image_size' => 48, 49 | 50 | /* 51 | |-------------------------------------------------------------------------- 52 | | Font size 53 | |-------------------------------------------------------------------------- 54 | | 55 | | Set the font size for the initials. Percentage, based on the image size. 56 | */ 57 | 'font_size' => 0.5, 58 | 59 | /* 60 | |-------------------------------------------------------------------------- 61 | | Rounded image 62 | |-------------------------------------------------------------------------- 63 | | 64 | | Set if the returned image should be rounded or square. 65 | | It's recommended to use CSS to round the image for performance. 66 | */ 67 | 'rounded' => false, 68 | 69 | /* 70 | |-------------------------------------------------------------------------- 71 | | Smooth rounding 72 | |-------------------------------------------------------------------------- 73 | | 74 | | This is only possible if 'rounded' is true and with 'local' provider. 75 | | 76 | | Will use a hack to make the rounded circle appear much sharper. Can be a 77 | | performance hit as the image will be generated at a larger size and 78 | | scaled down to fit. 79 | */ 80 | 'smooth_rounding' => true, 81 | 82 | /* 83 | |-------------------------------------------------------------------------- 84 | | Uppercase letters 85 | |-------------------------------------------------------------------------- 86 | | 87 | | Set if the initials should be uppercased. 88 | */ 89 | 'uppercase' => true, 90 | 91 | /* 92 | |-------------------------------------------------------------------------- 93 | | Background Color 94 | |-------------------------------------------------------------------------- 95 | | 96 | | Set the HEX for the background color. 97 | */ 98 | 'background_color' => '#a0a0a0', 99 | 100 | /* 101 | |-------------------------------------------------------------------------- 102 | | Font Color 103 | |-------------------------------------------------------------------------- 104 | | 105 | | Set the HEX for the font color. 106 | */ 107 | 'font_color' => '#222', 108 | 109 | /* 110 | |-------------------------------------------------------------------------- 111 | | Font Weight 112 | |-------------------------------------------------------------------------- 113 | | 114 | | Prefer bold fonts 115 | */ 116 | 'font_bold' => false, 117 | 118 | /* 119 | |-------------------------------------------------------------------------- 120 | | Providers 121 | |-------------------------------------------------------------------------- 122 | | 123 | | List of available providers 124 | */ 125 | 'providers' => [ 126 | 'api' => Rackbeat\UIAvatars\Generators\ApiGenerator::class, 127 | 'local' => Rackbeat\UIAvatars\Generators\LocalGenerator::class, 128 | ], 129 | ]; -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | ./vendor 23 | ./tests 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/Avatar.php: -------------------------------------------------------------------------------- 1 | name( $name )->image(); 29 | } 30 | 31 | /** 32 | * @param string $name 33 | * 34 | * @return SVG|string 35 | */ 36 | public static function svg( $name = '' ) { 37 | return static::make()->name( $name )->svg(); 38 | } 39 | 40 | /** 41 | * @param string $name 42 | * 43 | * @return string 44 | */ 45 | public static function base64( $name = '' ) { 46 | return static::make()->name( $name )->base64(); 47 | } 48 | 49 | /** 50 | * @param string $name 51 | * 52 | * @return string 53 | */ 54 | public static function initials( $name = '' ) { 55 | return static::make()->name( $name )->initials(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/AvatarGeneratorFactory.php: -------------------------------------------------------------------------------- 1 | name( $name ); 16 | } 17 | 18 | /** 19 | * @param string $provider 20 | * 21 | * @return AvatarGeneratorInterface 22 | */ 23 | public static function select( $provider = 'local' ) { 24 | $providerClass = config( "ui-avatars.providers.{$provider}" ); 25 | 26 | return new $providerClass; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/AvatarGeneratorInterface.php: -------------------------------------------------------------------------------- 1 | length( config( 'ui-avatars.length' ) ); 14 | $this->fontSize( config( 'ui-avatars.font_size' ) ); 15 | $this->imageSize( config( 'ui-avatars.image_size' ) ); 16 | $this->rounded( (bool) config( 'ui-avatars.rounded' ) ); 17 | $this->uppercase( (bool) config( 'ui-avatars.uppercase' ) ); 18 | $this->backgroundColor( config( 'ui-avatars.background_color' ) ); 19 | $this->fontColor( config( 'ui-avatars.font_color' ) ); 20 | $this->bold( (bool) config( 'ui-avatars.font_bold' ) ); 21 | $this->options['region'] = config('ui-avatars.default_region'); 22 | } 23 | 24 | public function region( $region ) { 25 | $this->options['region'] = $region; 26 | 27 | return $this; 28 | } 29 | 30 | public function name( $name ) { 31 | $this->options['name'] = $name; 32 | 33 | return $this; 34 | } 35 | 36 | public function length( $length ) { 37 | $this->options['length'] = $length; 38 | 39 | return $this; 40 | } 41 | 42 | public function fontSize( $fontSize ) { 43 | $this->options['font-size'] = $fontSize; 44 | 45 | return $this; 46 | } 47 | 48 | public function imageSize( $imageSize ) { 49 | // Option to specify custom, or default to config. 50 | if ( $imageSize === null ) { 51 | return $this; 52 | } 53 | 54 | $this->options['size'] = $imageSize; 55 | 56 | return $this; 57 | } 58 | 59 | public function rounded( $rounded ) { 60 | $this->options['rounded'] = $rounded; 61 | 62 | return $this; 63 | } 64 | 65 | public function fontColor( $fontColor ) { 66 | $this->options['color'] = str_replace( '#', '', $fontColor ); 67 | 68 | return $this; 69 | } 70 | 71 | public function backgroundColor( $backgroundColor ) { 72 | $this->options['background'] = str_replace( '#', '', $backgroundColor ); 73 | 74 | return $this; 75 | } 76 | 77 | public function uppercase( $uppercase ) { 78 | $this->options['uppercase'] = $uppercase; 79 | 80 | return $this; 81 | } 82 | 83 | public function bold( $bold ) { 84 | $this->options['bold'] = $bold; 85 | 86 | return $this; 87 | } 88 | 89 | public function base64() { 90 | return $this->image(); 91 | } 92 | 93 | public function image() { 94 | return $this->getHost() . '/api/?' . http_build_query( $this->options ); 95 | } 96 | 97 | public function svg() { 98 | return $this->getHost() . '/svg/?' . http_build_query( $this->options ); 99 | } 100 | 101 | public function urlfriendly() { 102 | return urlencode( $this->getHost() . '/api' 103 | . '/' . urlencode( $this->options['name'] ) 104 | . '/' . $this->options['size'] 105 | . '/' . $this->options['background'] 106 | . '/' . $this->options['color'] 107 | . '/' . $this->options['length'] 108 | . '/' . $this->options['font-size'] 109 | . '/' . $this->options['rounded'] 110 | . '/' . $this->options['uppercase'] 111 | . '/' . ( $this->options['bold'] ?? false ) ); 112 | } 113 | 114 | public function initials( $length = null ) { 115 | return ( new InitialAvatar )->name( $this->options['name'] )->length( $length ?: $this->options['length'] )->getInitials(); 116 | } 117 | 118 | protected function getHost() { 119 | if ( empty( $this->options['region'] ) ) { 120 | return 'https://ui-avatars.com'; 121 | } 122 | 123 | return sprintf( 'https://%s.ui-avatars.com', $this->options['region'] ); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Generators/LocalGenerator.php: -------------------------------------------------------------------------------- 1 | service = new InitialAvatar(); 15 | 16 | $this->length( config( 'ui-avatars.length' ) ); 17 | $this->fontSize( config( 'ui-avatars.font_size' ) ); 18 | $this->imageSize( config( 'ui-avatars.image_size' ) ); 19 | $this->rounded( (bool) config( 'ui-avatars.rounded' ) ); 20 | $this->smooth( (bool) config( 'ui-avatars.smooth_rounding' ) ); 21 | $this->uppercase( (bool) config( 'ui-avatars.uppercase' ) ); 22 | $this->backgroundColor( config( 'ui-avatars.background_color' ) ); 23 | $this->fontColor( config( 'ui-avatars.font_color' ) ); 24 | $this->bold( (bool) config( 'ui-avatars.font_bold' ) ); 25 | } 26 | 27 | public function name( $name ) { 28 | $this->service->name( $name ); 29 | 30 | return $this; 31 | } 32 | 33 | public function length( $length ) { 34 | $this->service->length( $length ); 35 | 36 | return $this; 37 | } 38 | 39 | public function fontSize( $fontSize ) { 40 | $this->service->fontSize( $fontSize ); 41 | 42 | return $this; 43 | } 44 | 45 | public function imageSize( $imageSize ) { 46 | // Option to specify custom, or default to config. 47 | if ( $imageSize === null ) { 48 | return $this; 49 | } 50 | 51 | $this->service->size( $imageSize ); 52 | 53 | return $this; 54 | } 55 | 56 | public function rounded( $rounded ) { 57 | $this->service->rounded( $rounded ); 58 | 59 | return $this; 60 | } 61 | 62 | 63 | public function smooth( $smooth ) { 64 | $this->service->smooth( $smooth ); 65 | 66 | return $this; 67 | } 68 | 69 | public function fontColor( $fontColor ) { 70 | $this->service->color( $fontColor ); 71 | 72 | return $this; 73 | } 74 | 75 | public function backgroundColor( $backgroundColor ) { 76 | $this->service->background( $backgroundColor ); 77 | 78 | return $this; 79 | } 80 | 81 | public function uppercase( $uppercase ) { 82 | $this->service->keepCase( ! $uppercase ); 83 | 84 | return $this; 85 | } 86 | 87 | public function bold( $bold ) { 88 | if ( $bold ) { 89 | $this->service->preferBold(); 90 | } 91 | 92 | return $this; 93 | } 94 | 95 | public function base64() { 96 | return $this->stream( 'data-url', 100 ); 97 | } 98 | 99 | public function stream( $format = 'png', $quality = 100 ) { 100 | return $this->image()->stream( $format, $quality ); 101 | } 102 | 103 | public function urlfriendly() { 104 | return $this->base64(); 105 | } 106 | 107 | public function image() { 108 | return $this->service->generate(); 109 | } 110 | 111 | public function svg() { 112 | return $this->service->generateSvg()->toXMLString(); 113 | } 114 | 115 | public function initials( $length = null ) { 116 | if ( $length !== null ) { 117 | $this->length( $length ); 118 | } 119 | 120 | return $this->service->getInitials(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/HasAvatar.php: -------------------------------------------------------------------------------- 1 | {$this->getAvatarNameKey()}; 16 | } 17 | 18 | /** 19 | * Get the key on the model to grab the name from. 20 | * 21 | * Defaults to 'name' for App\User model. 22 | * 23 | * @return string 24 | */ 25 | protected function getAvatarNameKey() { 26 | return 'name'; 27 | } 28 | 29 | /** 30 | * @return AvatarGeneratorInterface 31 | */ 32 | public function getAvatarGenerator() { 33 | return AvatarGeneratorFactory::make( $this->getAvatarName() ); 34 | } 35 | 36 | /** 37 | * @param null|int $length 38 | * 39 | * @return string 40 | */ 41 | public function getInitials( $length = null ) { 42 | return $this->getAvatarGenerator()->initials( $length ); 43 | } 44 | 45 | /** 46 | * @param null|int $size 47 | * 48 | * @return Image 49 | */ 50 | public function getAvatarImage( $size = null ) { 51 | return $this->getAvatarGenerator()->imageSize( $size )->image(); 52 | } 53 | 54 | /** 55 | * @param null|int $size 56 | * 57 | * @return string 58 | */ 59 | public function getAvatarSvg( $size = null ) { 60 | return $this->getAvatarGenerator()->imageSize( $size )->svg(); 61 | } 62 | 63 | 64 | /** 65 | * @param null|int $size 66 | * 67 | * @return string 68 | */ 69 | public function getAvatarBase64( $size = null ) { 70 | return $this->getAvatarGenerator()->imageSize( $size )->base64(); 71 | } 72 | 73 | /** 74 | * Returns a string valid to use as a Gravatar (or similar) fallback. 75 | * 76 | * ONLY USEFUL FOR 'api' provider. 77 | * 78 | * @param null|int $size 79 | * 80 | * @return string 81 | */ 82 | public function getUrlfriendlyAvatar( $size = null ) { 83 | return $this->getAvatarGenerator()->imageSize( $size )->urlfriendly(); 84 | } 85 | 86 | /** 87 | * Returns a gravatar url. 88 | * 89 | * ONLY WORKS FOR 'api' provider. 90 | * 91 | * @param string email 92 | * @param null|int $size 93 | * 94 | * @return string 95 | */ 96 | public function getGravatar( $email, $size = null ) { 97 | return 'https://www.gravatar.com/avatar/' . md5( strtolower( $email ) ) . '?s=' . $size . '&default=' . $this->getUrlfriendlyAvatar( $size ); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/UIAvatarsServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes( [ 14 | __DIR__ . '/../config/ui-avatars.php' => config_path( 'ui-avatars.php' ), 15 | ], 'config' ); 16 | } 17 | 18 | /** 19 | * @return void 20 | */ 21 | public function register() { 22 | $this->mergeConfigFrom( __DIR__ . '/../config/ui-avatars.php', 'ui-avatars' ); 23 | } 24 | } 25 | --------------------------------------------------------------------------------