Skip to content

Commit 230df50

Browse files
committed
remove flexible font locator, only allow fixed path
1 parent ee351de commit 230df50

File tree

7 files changed

+25
-62
lines changed

7 files changed

+25
-62
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## 2.0.0
4+
* support plain PHP project
5+
* remove support for php 5.x
6+
* remove font folder publishing (Laravel version)
7+
* remove `setFontFolder`
8+
* `fonts` config only accept full path

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ return [
9797

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

103102
// List of foreground colors to be used, randomly selected based on name supplied
104103
'foregrounds' => [

config/config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141

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

4746
// List of foreground colors to be used, randomly selected based on name supplied
4847
'foregrounds' => [
File renamed without changes.
File renamed without changes.

src/Avatar.php

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class Avatar
3535

3636
protected $initialGenerator;
3737

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

4140
/**
4241
* Avatar constructor.
@@ -73,8 +72,8 @@ public function __construct(array $config = [], Repository $cache = null, Initia
7372
$this->availableBackgrounds = $config['backgrounds'];
7473
$this->availableForegrounds = $config['foregrounds'];
7574
$this->fonts = $config['fonts'];
75+
$this->font = $this->defaultFont;
7676
$this->fontSize = $config['fontSize'];
77-
$this->fontFolder = [__DIR__.'/../resources/assets/fonts/'];
7877
$this->width = $config['width'];
7978
$this->height = $config['height'];
8079
$this->ascii = $config['ascii'];
@@ -101,28 +100,19 @@ public function __construct(array $config = [], Repository $cache = null, Initia
101100
*/
102101
public function __toString()
103102
{
104-
return (string) $this->toBase64();
103+
return (string)$this->toBase64();
105104
}
106105

107106
public function create($name)
108107
{
109108
$this->name = $name;
110109

111-
$this->initialGenerator->setName($name);
112-
$this->initialGenerator->setLength($this->chars);
113-
$this->initials = $this->initialGenerator->make();
114-
115110
$this->setForeground($this->getRandomForeground());
116111
$this->setBackground($this->getRandomBackground());
117112

118113
return $this;
119114
}
120115

121-
public function setFontFolder($folders)
122-
{
123-
$this->fontFolder = $folders;
124-
}
125-
126116
public function setFont($font)
127117
{
128118
if (is_file($font)) {
@@ -222,26 +212,9 @@ protected function getRandomForeground()
222212

223213
protected function setRandomFont()
224214
{
225-
$this->font = $this->defaultFont;
226-
227-
$initials = $this->getInitial();
228-
229-
if ($initials) {
230-
$number = ord($initials[0]);
231-
$font = $this->fonts[$number % count($this->fonts)];
215+
$randomFont = $this->getRandomElement($this->fonts, $this->defaultFont);
232216

233-
if (!is_array($this->fontFolder)) {
234-
throw new \Exception('Font folder not set');
235-
}
236-
237-
foreach ($this->fontFolder as $folder) {
238-
$fontFile = $folder.$font;
239-
if (is_file($fontFile)) {
240-
$this->font = $fontFile;
241-
break;
242-
}
243-
}
244-
}
217+
$this->setFont($randomFont);
245218
}
246219

247220
protected function getBorderColor()
@@ -258,6 +231,8 @@ protected function getBorderColor()
258231

259232
protected function buildAvatar()
260233
{
234+
$this->buildInitial();
235+
261236
$x = $this->width / 2;
262237
$y = $this->height / 2;
263238

@@ -350,7 +325,7 @@ protected function cacheKey()
350325

351326
protected function getRandomElement($array, $default)
352327
{
353-
if (strlen($this->name) == 0) {
328+
if (strlen($this->name) == 0 || count($array) == 0) {
354329
return $default;
355330
}
356331

@@ -371,4 +346,11 @@ protected function chooseFont()
371346
$this->setRandomFont();
372347
}
373348
}
349+
350+
protected function buildInitial()
351+
{
352+
$this->initialGenerator->setName($this->name);
353+
$this->initialGenerator->setLength($this->chars);
354+
$this->initials = $this->initialGenerator->make();
355+
}
374356
}

src/ServiceProvider.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ public function register()
2727

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

30-
// list of folder to scan where font located, order by priority
31-
$fontFolder = [
32-
// no folder at all, allow developer to supply full path to file in their configuration
33-
'',
34-
35-
// find file located in published asset folder
36-
base_path('resources/laravolt/avatar/fonts/'),
37-
38-
// find font included by default in package
39-
__DIR__.'/../resources/assets/fonts/',
40-
];
41-
42-
$avatar->setFontFolder($fontFolder);
43-
4430
return $avatar;
4531
});
4632
}
@@ -52,20 +38,9 @@ public function register()
5238
*/
5339
public function boot()
5440
{
55-
$this->registerAssets();
5641
$this->registerConfigurations();
5742
}
5843

59-
/**
60-
* Register the package public assets.
61-
*
62-
* @return void
63-
*/
64-
protected function registerAssets()
65-
{
66-
$this->publishes([$this->packagePath('resources/assets') => base_path('resources/laravolt/avatar')], 'assets');
67-
}
68-
6944
/**
7045
* Register the package configurations.
7146
*

0 commit comments

Comments
 (0)