Skip to content

Commit

Permalink
Merge pull request #9 from sowrensen/dev
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
sowrensen authored Mar 24, 2023
2 parents b0631a1 + c749726 commit b72d1fd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to `svg-avatar-generator` will be documented in this file.

## 1.3.0

- Support for rounded corners.
- Support for route middleware.
- New keys `corner_radius` and `middleware` in config.

## 1.2.0

- Support for custom extractor.
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/sowrensen/svg-avatar-generator/run-tests.yml?branch=main&label=Tests)](https://github.com/sowrensen/svg-avatar-generator/actions?query=workflow%3ATests+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/sowrensen/svg-avatar-generator.svg)](https://packagist.org/packages/sowrensen/svg-avatar-generator)

> If you find this package useful, please consider to ⭐ it. That would be lit!
Generating SVG avatars on the fly is nothing new. There are tons of free/paid services and packages available to do that. So, why
another package for same task?

Expand All @@ -15,6 +17,7 @@ Well, this one has some subtle but nifty advantages over available packages, her
- [x] Doesn't have any binary dependency, so nothing needs to be installed on server. 🗃️
- [x] Supports gradient background. 🦜
- [x] Supports random gradients based on defined presets in config. 🦚
- [x] Multiple shapes: rectangular, rounded-rectangular, or circular. 💎
- [x] Ability to customize initials and extractor. ✍🏼

## Requirements
Expand Down Expand Up @@ -80,7 +83,7 @@ use Sowren\SvgAvatarGenerator\Facades\Svg;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;

Svg::for('John Doe')
->asCircle() // or, asRectangle()
->asCircle() // or, asRectangle() along with optional setCornerRadius($radius) method
->setSize(64)
->setFontSize(40)
->setFontWeight(FontWeight::SEMIBOLD)
Expand Down Expand Up @@ -135,8 +138,7 @@ After doing that, set the class as default extractor in config.

## Sample Output

<img src="https://user-images.githubusercontent.com/13097375/221879852-b8283a4a-f3ff-42a9-b37a-07cbc9bd0afe.png" height="128"/>

<img src="https://user-images.githubusercontent.com/13097375/227495887-99a0430f-925f-4f1f-acac-0dca21686bfd.png" height="128"/>

## Testing

Expand Down
32 changes: 31 additions & 1 deletion config/svg-avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,23 @@
| Allowed: Shape::CIRCLE or Shape::RECTANGLE
|
*/
'shape' => \Sowren\SvgAvatarGenerator\Enums\Shape::CIRCLE,
'shape' => \Sowren\SvgAvatarGenerator\Enums\Shape::RECTANGLE,

/*
|--------------------------------------------------------------------------
| Corner Radius
|--------------------------------------------------------------------------
|
| You can define the corner radius of the Rectangle shape to achieve a
| nice rounder corner. This setting only has effect when Shape is
| set to Rectangle.
|
| Type: int
| Default: 0
| Allowed: 0 to 25
|
*/
'corner_radius' => 0,

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -182,4 +198,18 @@
|
*/
'url' => 'svg-avatar',

/*
|--------------------------------------------------------------------------
| Middleware
|--------------------------------------------------------------------------
|
| The route is public by default. To prevent unwanted and unauthenticated
| usage, you can set a middleware (e.g., 'auth', or ['auth']) here.
|
| Type: array<string>|string
| Default: null
|
*/
'middleware' => null,
];
8 changes: 7 additions & 1 deletion resources/views/elements/rectangle.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<rect width='100%' height='100%' fill='url(#{{ $gradientId }})'/>
@php
/**
* @var Sowren\SvgAvatarGenerator\SvgAvatarGenerator $generator;
*/
@endphp

<rect width='100%' height='100%' fill='url(#{{ $gradientId }})' rx="{{ $generator->getCornerRadius() }}"/>
4 changes: 2 additions & 2 deletions resources/views/elements/svg.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</defs>
@include($generator->getShape()->render())
<text
x="50%" y="50%" style="line-height: 1;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;"
x="50%" y="50%" style="line-height: 1;"
font-family="-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif"
alignment-baseline="middle" text-anchor="middle"
font-size="{{ $generator->getFontSize() }}"
font-weight="{{ $generator->getFontWeight()->value }}"
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidCornerRadius.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sowren\SvgAvatarGenerator\Exceptions;

class InvalidCornerRadius extends \Exception
{
public static function create(int $radius): InvalidCornerRadius
{
return new self("Invalid corner radius {$radius} is provided. Corner radius should be between 0 to 25.");
}
}
10 changes: 9 additions & 1 deletion src/Http/Controllers/SvgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidCornerRadius;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidFontSizeException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientRotationException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientStopException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidSvgSizeException;
use Sowren\SvgAvatarGenerator\Exceptions\MissingTextException;
use Sowren\SvgAvatarGenerator\SvgAvatarGenerator;

class SvgController
class SvgController extends Controller
{
public function __construct()
{
$this->middleware(config('svg-avatar.middleware'));
}

/**
* Generate an SVG and send it as an HTTP response.
*
* @throws InvalidCornerRadius
* @throws MissingTextException
* @throws InvalidSvgSizeException
* @throws InvalidFontSizeException
Expand Down
30 changes: 30 additions & 0 deletions src/SvgAvatarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Sowren\SvgAvatarGenerator\Concerns\Tool;
use Sowren\SvgAvatarGenerator\Enums\FontWeight;
use Sowren\SvgAvatarGenerator\Enums\Shape;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidCornerRadius;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidFontSizeException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientRotationException;
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientStopException;
Expand All @@ -32,6 +33,8 @@ class SvgAvatarGenerator
*/
protected Shape $shape;

protected int $cornerRadius;

/**
* Font size of the SVG.
*/
Expand Down Expand Up @@ -78,6 +81,7 @@ class SvgAvatarGenerator
public Extractor $extractor;

/**
* @throws InvalidCornerRadius
* @throws InvalidSvgSizeException
* @throws InvalidFontSizeException
* @throws InvalidGradientStopException
Expand Down Expand Up @@ -186,6 +190,30 @@ public function asRectangle(): static
return $this;
}

/**
* Get corner radius of rectangular shape.
*/
public function getCornerRadius(): int
{
return $this->cornerRadius;
}

/**
* Set corner radius of rectangular shape.
*
* @throws InvalidCornerRadius
*/
public function setCornerRadius(int $radius): static
{
if ($radius < 0 || $radius > 25) {
throw InvalidCornerRadius::create($radius);
}

$this->cornerRadius = $radius;

return $this;
}

/**
* Get the font size.
*/
Expand Down Expand Up @@ -350,6 +378,7 @@ protected function setGradientSet(): static
/**
* Set default values from config.
*
* @throws InvalidCornerRadius
* @throws InvalidFontSizeException
* @throws InvalidGradientRotationException
* @throws InvalidSvgSizeException
Expand All @@ -360,6 +389,7 @@ protected function build(): void
$this
->setSize($this->config['size'])
->setShape($this->config['shape'])
->setCornerRadius($this->config['corner_radius'])
->setFontSize($this->config['font_size'])
->setFontWeight($this->config['font_weight'])
->setForeground($this->config['foreground'])
Expand Down

0 comments on commit b72d1fd

Please sign in to comment.