From a144b4f8d85facd8915502f677fa90370d5afd71 Mon Sep 17 00:00:00 2001 From: uyab Date: Wed, 21 Oct 2015 11:35:15 +0700 Subject: [PATCH] configurable shape (circle or square) --- README.md | 32 +++++++++++++++---- config/config.php | 13 ++++++++ src/Avatar.php | 78 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 111 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2a65f13..849c644 100644 --- a/README.md +++ b/README.md @@ -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', @@ -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' ] ]; @@ -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(); diff --git a/config/config.php b/config/config.php index 5235b1f..114f312 100644 --- a/config/config.php +++ b/config/config.php @@ -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, @@ -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' ] ]; diff --git a/src/Avatar.php b/src/Avatar.php index a40b813..f30cdae 100644 --- a/src/Avatar.php +++ b/src/Avatar.php @@ -8,6 +8,7 @@ class Avatar { + protected $shape; protected $name; protected $chars; protected $availableBackgrounds; @@ -19,6 +20,8 @@ class Avatar protected $image; protected $background = '#cccccc'; protected $foreground = '#ffffff'; + protected $borderSize = 0; + protected $borderColor; protected $initials = ''; protected $ascii = false; @@ -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]); @@ -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) @@ -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(); } @@ -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)); @@ -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); @@ -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()); + }); + } }