Skip to content

Commit

Permalink
cache avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Oct 22, 2015
1 parent a144b4f commit 03cfc30
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php" : ">=5.5.0",
"illuminate/support": "~5.1",
"illuminate/cache": "~5.1",
"intervention/image": "2.3.2",
"danielstjules/stringy": "~1.8"
},
Expand Down
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

'border' => [
'size' => 1,

// border color, available value are:
// 'foreground' (same as foreground color)
// 'background' (same as background color)
Expand Down
37 changes: 27 additions & 10 deletions src/Avatar.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace Laravolt\Avatar;

use Stringy\Stringy;
use Illuminate\Support\Arr;
use Illuminate\Cache\CacheManager;
use Illuminate\Support\Collection;
use Intervention\Image\Facades\Image;
use Stringy\Stringy;

class Avatar
{
Expand All @@ -14,6 +15,7 @@ class Avatar
protected $availableBackgrounds;
protected $availableForegrounds;
protected $fonts;
protected $font;
protected $fontSize;
protected $width;
protected $height;
Expand All @@ -25,11 +27,14 @@ class Avatar
protected $initials = '';
protected $ascii = false;

protected $cache;

/**
* Avatar constructor.
* @param array $config
* @param CacheManager $cache
*/
public function __construct(array $config)
public function __construct(array $config, CacheManager $cache)
{
$this->shape = Arr::get($config, 'shape', 'circle');
$this->chars = Arr::get($config, 'chars', 2);
Expand All @@ -42,6 +47,8 @@ public function __construct(array $config)
$this->ascii = Arr::get($config, 'ascii', false);
$this->borderSize = Arr::get($config, 'border.size');
$this->borderColor = Arr::get($config, 'border.color');

$this->cache = $cache;
}

public function create($name)
Expand All @@ -62,6 +69,7 @@ public function create($name)
}

$this->initials = $this->getInitials();
$this->setFont();
$this->setForeground($this->getRandomForeground());
$this->setBackground($this->getRandomBackground());

Expand All @@ -70,9 +78,18 @@ public function create($name)

public function toBase64()
{
$this->buildAvatar();
$keys = [];
$attributes = ['initials', 'shape', 'chars', 'font', 'fontSize', 'width', 'height', 'borderSize', 'borderColor'];
foreach ($attributes as $attr) {
$keys[] = $this->$attr;
}

$cacheKey = md5(implode('-', $keys));

return $this->image->encode('data-url');
return $this->cache->rememberForever($cacheKey, function(){
$this->buildAvatar();
return $this->image->encode('data-url');
});
}

public function setBackground($hex)
Expand Down Expand Up @@ -178,7 +195,7 @@ protected function getRandomForeground()
return $this->availableForegrounds[$number % count($this->availableForegrounds)];
}

protected function getFont()
protected function setFont()
{
$initials = $this->getInitials();

Expand All @@ -187,11 +204,12 @@ protected function getFont()
$font = $this->fonts[$number % count($this->fonts)];
$fontFile = base_path('resources/laravolt/avatar/fonts/' . $font);
if (is_file($fontFile)) {
return $fontFile;
$this->font = $fontFile;
return true;
}
}

return 5;
$this->font = 5;
}

protected function getBorderColor()
Expand All @@ -215,9 +233,8 @@ protected function buildAvatar()

$this->createShape();

$initials = $this->getInitials();
$this->image->text($initials, $x, $y, function ($font) {
$font->file($this->getFont());
$this->image->text($this->initials, $x, $y, function ($font) {
$font->file($this->font);
$font->size($this->fontSize);
$font->color($this->foreground);
$font->align('center');
Expand Down
3 changes: 2 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function register()
{
$this->app->bind('avatar',function($app){
$config = $app->make('config');
return new Avatar($config->get('avatar'));
$cache = $app->make('cache');
return new Avatar($config->get('avatar'), $cache);
});
}

Expand Down

0 comments on commit 03cfc30

Please sign in to comment.