Skip to content

Commit

Permalink
configurable shape (circle or square)
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Oct 21, 2015
1 parent c0fc1cd commit a144b4f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ return [
// Whether all characters supplied must be replaced with their closest ASCII counterparts
'ascii' => false,

// image width, in pixel
// Image shape: circle or square
'shape' => 'circle',

// Image width, in pixel
'width' => 100,

// image height, in pixel
// Image height, in pixel
'height' => 100,

// number of characters used as initials
// Number of characters used as initials. If name consists of single word, the first N character will be used
'chars' => 2,

// font size
'fontSize' => 48,

// Fonts used to render text.
// If contains more than one fonts, it will randomly choose which font used
// If contains more than one fonts, randomly selected based on name supplied
'fonts' => ['OpenSans-Bold.ttf', 'rockwell.ttf'],

// list of background colors to be used
'colors' => [
// List of foreground colors to be used, randomly selected based on name supplied
'foregrounds' => [
'#FFFFFF'
],

// List of background colors to be used, randomly selected based on name supplied
'backgrounds' => [
'#f44336',
'#E91E63',
'#9C27B0',
Expand All @@ -89,6 +97,16 @@ return [
'#FFC107',
'#FF9800',
'#FF5722',
],

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

// border color, available value are:
// 'foreground' (same as foreground color)
// 'background' (same as background color)
// or any valid hex ('#aabbcc')
'color' => 'foreground'
]
];

Expand All @@ -103,6 +121,8 @@ Avatar::create('Soekarno')->setDimension(100, 200); // width = 100, height = 200
Avatar::create('Soekarno')->setBackground('#001122');
Avatar::create('Soekarno')->setForeground('#999999');
Avatar::create('Soekarno')->setFontSize(72);
Avatar::create('Soekarno')->setBorder(1, '#aabbcc'); // size = 1, color = #aabbcc
Avatar::create('Soekarno')->setShape('square');

// chaining
Avatar::create('Habibie')->setDimension(50)->setFontSize(18)->toBase64();
Expand Down
13 changes: 13 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// Whether all characters supplied must be replaced with their closest ASCII counterparts
'ascii' => false,

// Image shape: circle or square
'shape' => 'circle',

// Image width, in pixel
'width' => 100,

Expand Down Expand Up @@ -44,5 +47,15 @@
'#FFC107',
'#FF9800',
'#FF5722',
],

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

// border color, available value are:
// 'foreground' (same as foreground color)
// 'background' (same as background color)
// or any valid hex ('#aabbcc')
'color' => 'foreground'
]
];
78 changes: 72 additions & 6 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Avatar
{
protected $shape;
protected $name;
protected $chars;
protected $availableBackgrounds;
Expand All @@ -19,6 +20,8 @@ class Avatar
protected $image;
protected $background = '#cccccc';
protected $foreground = '#ffffff';
protected $borderSize = 0;
protected $borderColor;
protected $initials = '';
protected $ascii = false;

Expand All @@ -28,6 +31,7 @@ class Avatar
*/
public function __construct(array $config)
{
$this->shape = Arr::get($config, 'shape', 'circle');
$this->chars = Arr::get($config, 'chars', 2);
$this->availableBackgrounds = Arr::get($config, 'backgrounds', [$this->background]);
$this->availableForegrounds = Arr::get($config, 'foregrounds', [$this->foreground]);
Expand All @@ -36,6 +40,8 @@ public function __construct(array $config)
$this->width = Arr::get($config, 'width', 100);
$this->height = Arr::get($config, 'height', 100);
$this->ascii = Arr::get($config, 'ascii', false);
$this->borderSize = Arr::get($config, 'border.size');
$this->borderColor = Arr::get($config, 'border.color');
}

public function create($name)
Expand All @@ -51,8 +57,7 @@ public function create($name)
}

$this->name = Stringy::create($name)->collapseWhitespace();
if ($this->ascii)
{
if ($this->ascii) {
$this->name = $this->name->toAscii();
}

Expand Down Expand Up @@ -102,6 +107,21 @@ public function setFontSize($size)
return $this;
}

public function setBorder($size, $color)
{
$this->borderSize = $size;
$this->borderColor = $color;

return $this;
}

public function setShape($shape)
{
$this->shape = $shape;

return $this;
}

protected function getInitials()
{
$words = new Collection(explode(' ', $this->name));
Expand Down Expand Up @@ -174,15 +194,28 @@ protected function getFont()
return 5;
}

protected function buildAvatar()
protected function getBorderColor()
{
$this->image = Image::canvas($this->width, $this->height);
$this->image->fill($this->background);
if ($this->borderColor == 'foreground') {
return $this->foreground;
}
if ($this->borderColor == 'background') {
return $this->background;
}

return $this->borderColor;
}

protected function buildAvatar()
{
$x = $this->width / 2;
$y = $this->height / 2;
$initials = $this->getInitials();

$this->image = Image::canvas($this->width, $this->height);

$this->createShape();

$initials = $this->getInitials();
$this->image->text($initials, $x, $y, function ($font) {
$font->file($this->getFont());
$font->size($this->fontSize);
Expand All @@ -192,4 +225,37 @@ protected function buildAvatar()
});

}

protected function createShape()
{
$method = 'create' . ucfirst($this->shape) . 'Shape';
if (method_exists($this, $method)) {
return $this->$method();
}

throw new \InvalidArgumentException("Shape [$this->shape] currently not supported.");
}

protected function createCircleShape()
{
$circleDiameter = $this->width - $this->borderSize;
$x = $this->width / 2;
$y = $this->height / 2;

$this->image->circle($circleDiameter, $x, $y, function ($draw) {
$draw->background($this->background);
$draw->border($this->borderSize, $this->getBorderColor());
});
}

protected function createSquareShape()
{
$x = $y = $this->borderSize;
$width = $this->width - ($this->borderSize * 2);
$height = $this->height - ($this->borderSize * 2);
$this->image->rectangle($x, $y, $width, $height, function ($draw) {
$draw->background($this->background);
$draw->border($this->borderSize, $this->getBorderColor());
});
}
}

0 comments on commit a144b4f

Please sign in to comment.