generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sowrensen/feat/multiple-gradient
Multiple gradient support
- Loading branch information
Showing
17 changed files
with
417 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<circle cx='50' cy='50' r='50' fill='url(#{{ $gradientId }})'/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@php | ||
/** | ||
* @var Sowren\SvgAvatarGenerator\SvgAvatarGenerator $generator | ||
* @var string $gradientId | ||
*/ | ||
@endphp | ||
|
||
<linearGradient id="{{ $gradientId }}" gradientTransform="rotate({{ $generator->getGradientRotation() }})"> | ||
@foreach($generator->getGradientSet() as $set) | ||
<stop offset="{{ $set['offset'] }}" stop-color="{{ $set['color'] }}"/> | ||
@endforeach | ||
</linearGradient> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<rect width='100%' height='100%' fill='url(#{{ $gradientId }})'/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@php | ||
/** | ||
* @var Sowren\SvgAvatarGenerator\SvgAvatarGenerator $generator | ||
* @var string $gradientId | ||
*/ | ||
@endphp | ||
|
||
<svg | ||
width="{{ $generator->getSize() }}" | ||
height="{{ $generator->getSize() }}" | ||
viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink" | ||
> | ||
<defs> | ||
@include('svg::elements.linear-gradient') | ||
</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;" | ||
alignment-baseline="middle" text-anchor="middle" | ||
font-size="{{ $generator->getFontSize() }}" | ||
font-weight="{{ $generator->getFontWeight()->value }}" | ||
dy=".1em" dominant-baseline="middle" | ||
fill="{{ $generator->getForeground() }}" | ||
> | ||
{{ $generator->getInitials() }} | ||
</text> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Sowren\SvgAvatarGenerator\Concerns; | ||
|
||
use Arr; | ||
use Exception; | ||
use Sowren\SvgAvatarGenerator\Exceptions\MissingTextException; | ||
use Str; | ||
|
||
trait Tool | ||
{ | ||
/** | ||
* Extract initials from given text/name. If only one word is given, | ||
* it will look for second capital character in the word, else | ||
* the consecutive second character will be taken. | ||
* | ||
* @throws MissingTextException | ||
*/ | ||
protected function extractInitials(): void | ||
{ | ||
$name = $this->text; | ||
|
||
if (Str::contains($name, ' ')) { | ||
// If name has more than one part then break each part upto each space | ||
$parts = Str::of($name)->explode(' '); | ||
} else { | ||
// If name has only one part then try to find out if there are | ||
// any uppercase letters in the string. Then break the string | ||
// upto each uppercase letters, this allows to pass names in | ||
// studly case, e.g. 'SowrenSen'. If no uppercase letter is | ||
// found, $parts will have only one item in the array. | ||
$parts = Str::of($name)->kebab()->replace('-', ' ')->explode(' '); | ||
} | ||
|
||
try { | ||
$firstInitial = $parts->first()[0]; | ||
|
||
// If only one part is found, take the second letter as second | ||
// initial, else take the first letter of the last part. | ||
$secondInitial = ($parts->count() === 1) ? $parts->first()[1] : $parts->last()[0]; | ||
} catch (Exception) { | ||
throw MissingTextException::create(); | ||
} | ||
|
||
$this->setInitials(strtoupper($firstInitial.$secondInitial)); | ||
} | ||
|
||
/** | ||
* Creates sets of colors and offsets to form the gradients. | ||
*/ | ||
public function zip(): array | ||
{ | ||
$colors = $this->getGradientColors(); | ||
$offsets = $this->getGradientStops(); | ||
|
||
$hasMultipleSet = count(Arr::where($colors, fn ($color) => is_array($color))) > 0; | ||
|
||
return $hasMultipleSet | ||
? $this->zipMultiple($colors, $offsets) | ||
: $this->zipOne($colors, $offsets); | ||
} | ||
|
||
/** | ||
* Zip one set of colors to offsets. | ||
*/ | ||
private function zipOne($colors, $offsets): array | ||
{ | ||
$set = []; | ||
|
||
foreach ($offsets as $key => $offset) { | ||
$set[] = ['color' => $colors[$key] ?? $colors[count($colors) - 1], 'offset' => $offset]; | ||
} | ||
|
||
return [$set]; | ||
} | ||
|
||
/** | ||
* Zip multiple sets of colors to offsets. | ||
*/ | ||
private function zipMultiple($colors, $offsets): array | ||
{ | ||
$gradientSets = []; | ||
foreach ($colors as $gradColors) { | ||
$set = []; | ||
foreach ($offsets as $key => $offset) { | ||
$color = is_array($gradColors) | ||
? $gradColors[$key] ?? $gradColors[count($gradColors) - 1] | ||
: $gradColors; | ||
|
||
$set[] = ['color' => $color, 'offset' => $offset]; | ||
} | ||
$gradientSets[] = $set; | ||
} | ||
|
||
return $gradientSets; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Sowren\SvgAvatarGenerator\Exceptions; | ||
|
||
class InvalidGradientStopException extends \Exception | ||
{ | ||
public static function create(array $offsets): InvalidGradientStopException | ||
{ | ||
$values = implode(',', $offsets); | ||
|
||
return new self("Invalid value [{$values}] for gradient stop is provided. Gradient stops must be between 0 and 1."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.