-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"illuminate/support": "^8.0|^9.0", | ||
"illuminate/view": "^8.0|^9.0" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"STS\\Laravatar\\LaravatarServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
return [ | ||
'gravatar' => [ | ||
// 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', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@props([ | ||
'first' => null, | ||
'last' => null, | ||
'email' => null, | ||
'avatarClass' => '', | ||
'size' => 100, | ||
'zIndexStart' => 1, | ||
]) | ||
|
||
<div {{ $attributes->merge(['class' => 'laravatar laravatar-stack', 'style' => 'position: relative;']) }}> | ||
<x-laravatar::initials | ||
:class="$avatarClass" | ||
style="width: {{ $size }}px; height: {{ $size }}px; position: absolute; z-index: {{ $zIndexStart }};" | ||
alt="" | ||
:first="$first" | ||
:last="$last" | ||
:size="$size * 2" | ||
/> | ||
|
||
<x-laravatar::gravatar | ||
:class="$avatarClass" | ||
style="width: {{ $size }}px; height: {{ $size }}px; position: absolute; z-index: {{ $zIndexStart + 1 }};" | ||
alt="" | ||
:email="$email" | ||
/> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<img src="{{ $url }}" {{ $attributes->merge(['class' => 'laravatar laravatar-gravatar']) }} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<svg {{ $attributes->merge(['class' => 'laravatar laravatar-fallback']) }} xmlns="http://www.w3.org/2000/svg" viewbox="0 0 122 122"> | ||
<rect x="-1" y="-1" width="122" height="122" fill="{{ $color }}"/> | ||
<text fill="#ffffff" x="60" y="80" font-size="52" font-family="{{ config('laravatar.fallback.font_family') }}" text-anchor="middle" font-weight="bold">{{ $initials }}</text> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace STS\Laravatar\Components; | ||
|
||
use Illuminate\View\Component; | ||
|
||
class Gravatar extends Component | ||
{ | ||
public $url; | ||
|
||
public function __construct($email = null, $size = 100, $default = null, $rating = null) | ||
{ | ||
$useDefault = !!$default ? $default : config('laravatar.gravatar.default'); | ||
$useRating = !!$rating ? $rating : config('laravatar.gravatar.rating'); | ||
|
||
$this->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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace STS\Laravatar\Components; | ||
|
||
use Illuminate\View\Component; | ||
|
||
class Initials extends Component | ||
{ | ||
public $color; | ||
|
||
public $initials; | ||
|
||
public function __construct($first = '', $last = '') | ||
{ | ||
$this->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 '??'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace STS\Laravatar; | ||
|
||
use Illuminate\Support\Facades\Blade; | ||
use Illuminate\Support\Facades\View; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravatarServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->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'); | ||
} | ||
} |