Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

prettifystudio/avatary

39 stars
2 code files
View prettifystudio/avatary on GitHub

app/Services/AvatarGenerator.php

Open in GitHub
use Illuminate\Support\Str;
use App\Service\ColorPicker;
use Intervention\Image\Gd\Font;
use Intervention\Image\Facades\Image;
use Intervention\Image\Image as ImageCanvas;
use Intervention\Image\Gd\Shapes\CircleShape;
use Intervention\Image\Gd\Shapes\RectangleShape;
 
class AvatarGenerator
{
public function __construct(
public string $name,
public string $background_color = "f44336",
public string $text_color ="fafafa",
public string $shape ="circle",
public int $size = 150,
) {
}
 
public function generateColor()
{
$this->background_color !== 'random' ?: $this->background_color = ColorPicker::pick() ;
return $this->background_color;
}
 
 
private function getName()
{
if (strlen($this->name) < 2) {
return Initials::generate('John Doe');
}
 
if (preg_match('/\p{Arabic}/u', $this->name)) {
return mb_strrev(Initials::generate($this->name));
}
 
return Initials::generate($this->name);
}
 
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');
}
}

app/Http/Controllers/Api/AvatarController.php

Open in GitHub
use Illuminate\Http\Request;
use App\Services\AvatarGenerator;
 
class AvatarController extends Controller
{
//
public function initials(Request $request)
{
$name = $request->query('name', 'John Doe');
$background_color = $request->query('bgcolor', 'random');
$text_color = $request->query('color', 'fafafa');
$shape = $request->query('shape', 'circle');
$size = $request->query('size', 260);
$image = new AvatarGenerator(name: $name, background_color:$background_color, text_color:$text_color, shape:$shape, size:$size);
return $image->generate();
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.