-
composer.json
Open in GitHub{ // "require": { // "intervention/image": "^2.6" }, // }
-
app/Services/AvatarGenerator.php
Open in GitHubuse Intervention\Image\Facades\Image; use Intervention\Image\Image as ImageCanvas; use Intervention\Image\Gd\Shapes\CircleShape; use Intervention\Image\Gd\Shapes\RectangleShape; class AvatarGenerator { // private function getShape() { if ($this->shape === 'circle') { return $this->drawrCircleShape(); } else { return $this->drawrRectangleShape(); } } private function initCanvas(): ImageCanvas { return Image::canvas($this->size * 2 + 6, $this->size * 2 + 6); } private function drawrRectangleShape(): ImageCanvas { $canvas = $this->initCanvas(); $canvas->rectangle(0, 0, $this->size * 2 + 6, $this->size * 2 + 6, function (RectangleShape $draw) { $draw->background($this->generateColor()); }); return $canvas; } private function drawrCircleShape(): ImageCanvas { $canvas = $this->initCanvas(); $canvas->circle($this->size*2, $this->size + 3, $this->size + 3, function (CircleShape $draw) { $draw->background($this->generateColor()); }); return $canvas; } private function getText(ImageCanvas $canvas) { $canvas->text($this->getName(), $this->size, $this->size, function (Font $font) { $font->file(public_path('/Cairo-Light.ttf')); $font->size($this->size); $font->color($this->text_color); $font->valign('middle'); $font->align('center'); $font->angle(360); }); return $canvas; } private function drawText() { $canvas = $this->getShape(); $canvas = $this->getText($canvas); return $canvas; } public function generate() { $canvas = $this->drawText(); $canvas->resize($this->size, $this->size); return $canvas->response('png'); } }