diff --git a/CHANGELOG.md b/CHANGELOG.md index 01649f2..38e64ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 2fdd5a4..ea94126 100644 --- a/README.md +++ b/README.md @@ -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? @@ -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 @@ -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) @@ -135,8 +138,7 @@ After doing that, set the class as default extractor in config. ## Sample Output - - + ## Testing diff --git a/config/svg-avatar.php b/config/svg-avatar.php index 68d23fc..4b3b56e 100644 --- a/config/svg-avatar.php +++ b/config/svg-avatar.php @@ -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, /* |-------------------------------------------------------------------------- @@ -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 + | Default: null + | + */ + 'middleware' => null, ]; diff --git a/resources/views/elements/rectangle.blade.php b/resources/views/elements/rectangle.blade.php index 5fe0755..735626a 100644 --- a/resources/views/elements/rectangle.blade.php +++ b/resources/views/elements/rectangle.blade.php @@ -1 +1,7 @@ - +@php + /** + * @var Sowren\SvgAvatarGenerator\SvgAvatarGenerator $generator; + */ +@endphp + + diff --git a/resources/views/elements/svg.blade.php b/resources/views/elements/svg.blade.php index 1dcaa14..e612893 100644 --- a/resources/views/elements/svg.blade.php +++ b/resources/views/elements/svg.blade.php @@ -16,8 +16,8 @@ @include($generator->getShape()->render()) middleware(config('svg-avatar.middleware')); + } + /** * Generate an SVG and send it as an HTTP response. * + * @throws InvalidCornerRadius * @throws MissingTextException * @throws InvalidSvgSizeException * @throws InvalidFontSizeException diff --git a/src/SvgAvatarGenerator.php b/src/SvgAvatarGenerator.php index dfa9eb3..16c6c91 100755 --- a/src/SvgAvatarGenerator.php +++ b/src/SvgAvatarGenerator.php @@ -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; @@ -32,6 +33,8 @@ class SvgAvatarGenerator */ protected Shape $shape; + protected int $cornerRadius; + /** * Font size of the SVG. */ @@ -78,6 +81,7 @@ class SvgAvatarGenerator public Extractor $extractor; /** + * @throws InvalidCornerRadius * @throws InvalidSvgSizeException * @throws InvalidFontSizeException * @throws InvalidGradientStopException @@ -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. */ @@ -350,6 +378,7 @@ protected function setGradientSet(): static /** * Set default values from config. * + * @throws InvalidCornerRadius * @throws InvalidFontSizeException * @throws InvalidGradientRotationException * @throws InvalidSvgSizeException @@ -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'])