From 8038a911051bfcf0b36bc53e90f670938fefc93a Mon Sep 17 00:00:00 2001 From: DP Date: Wed, 23 Feb 2022 12:04:01 -0500 Subject: [PATCH] everything --- composer.json | 28 ++++++++ config/laravatar.php | 43 ++++++++++++ .../views/components/avatar-stack.blade.php | 26 ++++++++ resources/views/components/gravatar.blade.php | 1 + resources/views/components/initials.blade.php | 4 ++ src/Components/Gravatar.php | 36 ++++++++++ src/Components/Initials.php | 66 +++++++++++++++++++ src/LaravatarServiceProvider.php | 24 +++++++ 8 files changed, 228 insertions(+) create mode 100644 composer.json create mode 100644 config/laravatar.php create mode 100644 resources/views/components/avatar-stack.blade.php create mode 100644 resources/views/components/gravatar.blade.php create mode 100644 resources/views/components/initials.blade.php create mode 100644 src/Components/Gravatar.php create mode 100644 src/Components/Initials.php create mode 100644 src/LaravatarServiceProvider.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b5314fa --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "stechstudio/laravatar", + "description": "Minimalist Blade component for displaying a Gravatar, or falling back to initials.", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "STS\\Laravatar\\": "src/" + } + }, + "authors": [ + { + "name": "David Palmer", + "email": "dp@reproconnect.com" + } + ], + "require": { + "illuminate/support": "^8.0|^9.0", + "illuminate/view": "^8.0|^9.0" + }, + "extra": { + "laravel": { + "providers": [ + "STS\\Laravatar\\LaravatarServiceProvider" + ] + } + } +} diff --git a/config/laravatar.php b/config/laravatar.php new file mode 100644 index 0000000..537539e --- /dev/null +++ b/config/laravatar.php @@ -0,0 +1,43 @@ + [ + // Gravatar users can set a rating to descibe their avatar's level of objectionable content. + // The ratings are based on US movie ratings: g, pg, r, x + // Each rating includes those below itself. e.g. allowing "R-rated" avatars will also + // allow PG and G-rated avatars. + // + // See Gravatar's documentation for specific guidelines: + // https://en.gravatar.com/site/implement/images/ + 'rating' => 'g', + + // What to display if no Gravatar exists. Will be passed as the 'd' parameter. + // + // See Gravatar's documentation for all available options: + // https://en.gravatar.com/site/implement/images/ + 'default' => 'blank', + ], + + 'fallback' => [ + // If a user does not have a Gravatar, the app will use one of the following colors + // as the background for their initials. + 'colors' => [ + '#191028', + '#46af45', + '#a1d685', + '#453e78', + '#7664fe', + '#833129', + '#9ec2e8', + '#dc534b', + '#e18d79', + '#d6b97b', + '#216c4b', + '#d365c8', + '#afaab9', + ], + + // If a user does not have a Gravatar, display their initials using these font families. + 'font_family' => 'Roboto, Helvetica, Arial, sans-serif', + ], +]; diff --git a/resources/views/components/avatar-stack.blade.php b/resources/views/components/avatar-stack.blade.php new file mode 100644 index 0000000..99dbd3b --- /dev/null +++ b/resources/views/components/avatar-stack.blade.php @@ -0,0 +1,26 @@ +@props([ + 'first' => null, + 'last' => null, + 'email' => null, + 'avatarClass' => '', + 'size' => 100, + 'zIndexStart' => 1, +]) + +
merge(['class' => 'laravatar laravatar-stack', 'style' => 'position: relative;']) }}> + + + +
diff --git a/resources/views/components/gravatar.blade.php b/resources/views/components/gravatar.blade.php new file mode 100644 index 0000000..725d02d --- /dev/null +++ b/resources/views/components/gravatar.blade.php @@ -0,0 +1 @@ +merge(['class' => 'laravatar laravatar-gravatar']) }} /> diff --git a/resources/views/components/initials.blade.php b/resources/views/components/initials.blade.php new file mode 100644 index 0000000..82c2aa4 --- /dev/null +++ b/resources/views/components/initials.blade.php @@ -0,0 +1,4 @@ +merge(['class' => 'laravatar laravatar-fallback']) }} xmlns="http://www.w3.org/2000/svg" viewbox="0 0 122 122"> + + {{ $initials }} + diff --git a/src/Components/Gravatar.php b/src/Components/Gravatar.php new file mode 100644 index 0000000..934ac4f --- /dev/null +++ b/src/Components/Gravatar.php @@ -0,0 +1,36 @@ +url = !!$email ? + '//www.gravatar.com/avatar/' . static::sanitizeEmail($email) . '?s=' . $size . '&d=' . $useDefault . '&rating=' . $useRating : + '//www.gravatar.com/avatar/NOHASH?s=' . $size . '&d=' . $useRating . '&f=y'; + } + + public function render() + { + return view('laravatar::components.gravatar'); + } + + /** + * Trims and lowercases an email address. + * + * @param string $email + * @return string + */ + private static function sanitizeEmail($email) + { + return md5(strtolower(trim($email))); + } +} diff --git a/src/Components/Initials.php b/src/Components/Initials.php new file mode 100644 index 0000000..6c04405 --- /dev/null +++ b/src/Components/Initials.php @@ -0,0 +1,66 @@ +initials = static::getInitials($first, $last); + $this->color = static::getColor($first . $last . $this->initials); + } + + public function render() + { + return view('laravatar::components.initials'); + } + + /** + * Return a color deterministically from a source string. + * + * @param string $source + * @return string + */ + public static function getColor($source) + { + $seed = 0; + + for ($i = 0; $i < strlen($source); $i++) { + $seed += ord(substr($source, $i, 1)); + } + srand($seed); + + $colors = config('laravatar.fallback.colors'); + return $colors[rand(0, count($colors) - 1)]; + } + + /** + * Return two initials or '??' from a first and/or last name. + * + * @param string $first + * @param string $last + * @return string + */ + public static function getInitials($first = '', $last = '') + { + if (!!$first && !!$last) { + return substr($first, 0, 1) . substr($last, 0, 1); + } + + if (!!$first) { + return substr($first, 0, 2); + } + + if (!!$last) { + return substr($last, 0, 2); + } + + return '??'; + } +} diff --git a/src/LaravatarServiceProvider.php b/src/LaravatarServiceProvider.php new file mode 100644 index 0000000..339455e --- /dev/null +++ b/src/LaravatarServiceProvider.php @@ -0,0 +1,24 @@ +mergeConfigFrom(realpath(__DIR__.'/../config/laravatar.php'), 'laravatar'); + } + + public function boot() + { + $this->publishes([realpath(__DIR__.'/../config/laravatar.php') => config_path('laravatar.php')]); + + $this->loadViewsFrom(realpath(__DIR__.'/../resources/views'), 'laravatar'); + View::addNamespace('laravatar', realpath(__DIR__.'/../resources/views')); + Blade::componentNamespace('STS\Laravatar\Components', 'laravatar'); + } +}