Skip to content

Commit

Permalink
remove flexible font locator, only allow fixed path
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Jul 4, 2017
1 parent ee351de commit 230df50
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 62 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,8 @@
# Changelog

## 2.0.0
* support plain PHP project
* remove support for php 5.x
* remove font folder publishing (Laravel version)
* remove `setFontFolder`
* `fonts` config only accept full path
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -97,8 +97,7 @@ return [

// Fonts used to render text.
// If contains more than one fonts, randomly selected based on name supplied
// You can provide absolute path, path relative to folder resources/laravolt/avatar/fonts/, or mixed.
'fonts' => ['OpenSans-Bold.ttf', 'rockwell.ttf'],
'fonts' => ['path/to/OpenSans-Bold.ttf', 'path/to/rockwell.ttf'],

// List of foreground colors to be used, randomly selected based on name supplied
'foregrounds' => [
Expand Down
3 changes: 1 addition & 2 deletions config/config.php
Expand Up @@ -41,8 +41,7 @@

// Fonts used to render text.
// If contains more than one fonts, randomly selected based on name supplied
// You can provide absolute path, path relative to folder resources/laravolt/avatar/fonts/, or mixed.
'fonts' => ['OpenSans-Bold.ttf', 'rockwell.ttf'],
'fonts' => [__DIR__.'/../fonts/OpenSans-Bold.ttf', __DIR__.'/../fonts/rockwell.ttf'],

// List of foreground colors to be used, randomly selected based on name supplied
'foregrounds' => [
Expand Down
File renamed without changes.
File renamed without changes.
48 changes: 15 additions & 33 deletions src/Avatar.php
Expand Up @@ -35,8 +35,7 @@ class Avatar

protected $initialGenerator;

protected $fontFolder;
protected $defaultFont = __DIR__.'/../resources/assets/fonts/OpenSans-Bold.ttf';
protected $defaultFont = __DIR__.'/../fonts/OpenSans-Bold.ttf';

/**
* Avatar constructor.
Expand Down Expand Up @@ -73,8 +72,8 @@ public function __construct(array $config = [], Repository $cache = null, Initia
$this->availableBackgrounds = $config['backgrounds'];
$this->availableForegrounds = $config['foregrounds'];
$this->fonts = $config['fonts'];
$this->font = $this->defaultFont;
$this->fontSize = $config['fontSize'];
$this->fontFolder = [__DIR__.'/../resources/assets/fonts/'];
$this->width = $config['width'];
$this->height = $config['height'];
$this->ascii = $config['ascii'];
Expand All @@ -101,28 +100,19 @@ public function __construct(array $config = [], Repository $cache = null, Initia
*/
public function __toString()
{
return (string) $this->toBase64();
return (string)$this->toBase64();
}

public function create($name)
{
$this->name = $name;

$this->initialGenerator->setName($name);
$this->initialGenerator->setLength($this->chars);
$this->initials = $this->initialGenerator->make();

$this->setForeground($this->getRandomForeground());
$this->setBackground($this->getRandomBackground());

return $this;
}

public function setFontFolder($folders)
{
$this->fontFolder = $folders;
}

public function setFont($font)
{
if (is_file($font)) {
Expand Down Expand Up @@ -222,26 +212,9 @@ protected function getRandomForeground()

protected function setRandomFont()
{
$this->font = $this->defaultFont;

$initials = $this->getInitial();

if ($initials) {
$number = ord($initials[0]);
$font = $this->fonts[$number % count($this->fonts)];
$randomFont = $this->getRandomElement($this->fonts, $this->defaultFont);

if (!is_array($this->fontFolder)) {
throw new \Exception('Font folder not set');
}

foreach ($this->fontFolder as $folder) {
$fontFile = $folder.$font;
if (is_file($fontFile)) {
$this->font = $fontFile;
break;
}
}
}
$this->setFont($randomFont);
}

protected function getBorderColor()
Expand All @@ -258,6 +231,8 @@ protected function getBorderColor()

protected function buildAvatar()
{
$this->buildInitial();

$x = $this->width / 2;
$y = $this->height / 2;

Expand Down Expand Up @@ -350,7 +325,7 @@ protected function cacheKey()

protected function getRandomElement($array, $default)
{
if (strlen($this->name) == 0) {
if (strlen($this->name) == 0 || count($array) == 0) {
return $default;
}

Expand All @@ -371,4 +346,11 @@ protected function chooseFont()
$this->setRandomFont();
}
}

protected function buildInitial()
{
$this->initialGenerator->setName($this->name);
$this->initialGenerator->setLength($this->chars);
$this->initials = $this->initialGenerator->make();
}
}
25 changes: 0 additions & 25 deletions src/ServiceProvider.php
Expand Up @@ -27,20 +27,6 @@ public function register()

$avatar = new Avatar($config->get('avatar'), $cache, new InitialGenerator());

// list of folder to scan where font located, order by priority
$fontFolder = [
// no folder at all, allow developer to supply full path to file in their configuration
'',

// find file located in published asset folder
base_path('resources/laravolt/avatar/fonts/'),

// find font included by default in package
__DIR__.'/../resources/assets/fonts/',
];

$avatar->setFontFolder($fontFolder);

return $avatar;
});
}
Expand All @@ -52,20 +38,9 @@ public function register()
*/
public function boot()
{
$this->registerAssets();
$this->registerConfigurations();
}

/**
* Register the package public assets.
*
* @return void
*/
protected function registerAssets()
{
$this->publishes([$this->packagePath('resources/assets') => base_path('resources/laravolt/avatar')], 'assets');
}

/**
* Register the package configurations.
*
Expand Down

0 comments on commit 230df50

Please sign in to comment.