Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Forms #8

Merged
merged 14 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Enum/FormTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Renfordt\Larvatar\Enum;

enum FormTypes: string
{
case Circle = 'circle';
case Square = 'square';
case Hexagon = 'hexagon';
}
93 changes: 87 additions & 6 deletions src/InitialsAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Renfordt\Larvatar;

use Renfordt\Larvatar\Enum\ColorType;
use Renfordt\Larvatar\Enum\FormTypes;
use SVG\Nodes\Shapes\SVGCircle;
use SVG\Nodes\Shapes\SVGPolygon;
use SVG\Nodes\Shapes\SVGRect;
use SVG\Nodes\Texts\SVGText;
use SVG\SVG;

Expand All @@ -14,6 +17,8 @@ class InitialsAvatar
private array $names = [];
private int $size = 128;
private int $fontSize = 0;
private FormTypes $form = FormTypes::Circle;
private int $rotation;

/**
* Create an instance of InitialsAvatar
Expand All @@ -35,9 +40,12 @@ public function setName(string $name): void
}

/**
* Generates the InitialsAvatar as an SVG
* @param array $names Array of Names which shall be shortend for the initials
* @return string Returns a SVG code of the Initials
* Generates an avatar based on the given names and encoding
*
* @param array $names An array of names to generate initials from
* @param string|null $encoding The encoding type for the output ('base64' or null)
*
* @return string The generated avatar in SVG format or the base64-encoded avatar image
*/
public function generate(array $names = [], string|null $encoding = null): string
{
Expand All @@ -46,15 +54,23 @@ public function generate(array $names = [], string|null $encoding = null): strin
$doc = $larvatar->getDocument();

$this->addFontIfNotEmpty();
$halfSize = $this->size / 2;

$color = $this->getColor($names);
list($darkColor, $lightColor) = $color->getColorSet();

$circle = $this->getCircle($halfSize, $lightColor);
if ($this->form == FormTypes::Circle) {
$halfSize = $this->size / 2;
$outlineForm = $this->getCircle($halfSize, $lightColor);
} elseif ($this->form == FormTypes::Square) {
$outlineForm = $this->getSquare($this->size, $lightColor);
} elseif ($this->form == FormTypes::Hexagon) {
$outlineForm = $this->getHexagon($this->size, $lightColor, $this->rotation);
}


$initials = $this->getInitials($names, $darkColor);

$doc->addChild($circle);
$doc->addChild($outlineForm);
$doc->addChild($initials);

if ($encoding == 'base64') {
Expand Down Expand Up @@ -129,6 +145,43 @@ private function getCircle(float $halfSize, Color $lightColor): SVGCircle
return $circle;
}

/**
* Get a square SVGRect
*
* @param float $size Half of the square size
* @param Color $lightColor The color of the square
*
* @return SVGRect The generated square SVGRect object
*/
private function getSquare(float $size, Color $lightColor): SVGRect
{
$square = new SVGRect(0, 0, $size, $size);
$square->setStyle('fill', $lightColor->getHex());
return $square;
}

/**
* Get a polygon shape
*
* @param float $size The size of the polygon
* @param Color $lightColor The light color to fill the polygon
* @return SVGPolygon The polygon shape with the specified size and color
*/
private function getHexagon(float $size, Color $lightColor, int $rotation = 0): SVGPolygon
{
$rotation = pi() / 180 * $rotation;

for ($i = 0; $i <= 5; $i++) {
$xCoordinate = $size / 2 * cos(pi() / 3 * $i+$rotation) + $size / 2;
$yCoordinate = $size / 2 * sin(pi() / 3 * $i+$rotation) + $size / 2;
$edgePoints[] = [$xCoordinate, $yCoordinate];
}

$polygon = new SVGPolygon($edgePoints);
$polygon->setStyle('fill', $lightColor->getHex());
return $polygon;
}

/**
* Generates initials for the given names and returns SVGText object
* @param array $names List of names
Expand Down Expand Up @@ -198,4 +251,32 @@ public function setFont(string $fontFamily, string $path): void
$this->fontFamily = $fontFamily;
$this->fontPath = $path;
}

/**
* Sets the form of the application
*
* @param string|FormTypes $form The form type
* @return void
*/
public function setForm(string|FormTypes $form): void
{
if (is_string($form)) {
$form = FormTypes::from($form);
}

$this->form = $form;
}

/**
* Sets the rotation angle of the element
*
* @param int $angle The rotation angle value
*
* @return void
*/
public function setRotation(int $angle): void
{
$this->rotation = $angle;
}

}
14 changes: 9 additions & 5 deletions src/Larvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Larvatar
protected string $font;
protected string $fontPath;
protected int $size = 100;
public InitialsAvatar $initialsAvatar;

/**
* Constructs a new instance of the class.
Expand All @@ -29,6 +30,10 @@ public function __construct(string $name = '', string $email = '', int|LarvatarT
} elseif ($type instanceof LarvatarTypes) {
$this->type = $type;
}

if ($this->type == LarvatarTypes::InitialsAvatar) {
$this->initialsAvatar = new InitialsAvatar($this->name);
}
}

/**
Expand All @@ -38,15 +43,14 @@ public function __construct(string $name = '', string $email = '', int|LarvatarT
public function getImageHTML(string $encoding = ''): string
{
if ($this->type == LarvatarTypes::InitialsAvatar) {
$initial_avatar = new InitialsAvatar($this->name);
if (isset($this->font) && $this->font != '' && $this->fontPath != '') {
$initial_avatar->setFont($this->font, $this->fontPath);
$this->initialsAvatar->setFont($this->font, $this->fontPath);
}
$initial_avatar->setSize($this->size);
$this->initialsAvatar->setSize($this->size);
if ($encoding == 'base64') {
return '<img src="'.$initial_avatar->generate(encoding: $encoding).'" />';
return '<img src="'.$this->initialsAvatar->generate(encoding: $encoding).'" />';
} else {
return $initial_avatar->generate();
return $this->initialsAvatar->generate();
}
}

Expand Down
86 changes: 85 additions & 1 deletion tests/InitialsAvatarTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Renfordt\Larvatar\Color;
use Renfordt\Larvatar\Enum\ColorType;
use Renfordt\Larvatar\Enum\FormTypes;
use Renfordt\Larvatar\InitialsAvatar;
use SVG\Nodes\Shapes\SVGPolygon;
use SVG\Nodes\Shapes\SVGRect;

final class InitialsAvatarTest extends TestCase
{
Expand Down Expand Up @@ -68,4 +73,83 @@ public function testGenerateWithBase64(): void
$base64
);
}
}

public function testGetSquare(): void
{
$initialsAvatar = new InitialsAvatar('Test Name');
$reflect = new \ReflectionClass($initialsAvatar);
$method = $reflect->getMethod('getSquare');

$color = new Color(ColorType::Hex, '#000000');

$result = $method->invoke($initialsAvatar, 128, $color);

$this->assertInstanceOf(SVGRect::class, $result);
$this->assertEquals(0, $result->getX());
$this->assertEquals(0, $result->getY());
$this->assertEquals(128, $result->getWidth());
$this->assertEquals(128, $result->getHeight());
$this->assertEquals('#000000', $result->getStyle('fill'));
}

public function testGetHexagon(): void
{
$initialsAvatar = new InitialsAvatar('Test Name');
$reflect = new \ReflectionClass($initialsAvatar);
$method = $reflect->getMethod('getHexagon');
$method->setAccessible(true);

$color = new Color(ColorType::Hex, '#000000');

$expectedPoints = [
[119.4256258422, 96],
[64, 128],
[8.5743741577959, 96],
[8.5743741577959, 32],
[64, 0],
[119.4256258422, 32]
];

$result = $method->invoke($initialsAvatar, 128, $color, 30);

$this->assertInstanceOf(SVGPolygon::class, $result);
$this->assertEquals('#000000', $result->getStyle('fill'));
$this->assertEquals($expectedPoints, $result->getPoints());
}

public function testSetRotation(): void
{
$initialsAvatar = new InitialsAvatar('Test Name');
$initialsAvatar->setRotation(45);
$reflector = new \ReflectionObject($initialsAvatar);
$property = $reflector->getProperty('rotation');
$property->setAccessible(true);
$this->assertEquals(45, $property->getValue($initialsAvatar));
}

public function testSetForm(): void
{
$initialsAvatar = new InitialsAvatar('Test Name');

$initialsAvatar->setForm('circle');
$reflector = new \ReflectionObject($initialsAvatar);
$property = $reflector->getProperty('form');
$property->setAccessible(true);
$this->assertEquals(FormTypes::Circle, $property->getValue($initialsAvatar));

$initialsAvatar->setForm('square');
$this->assertEquals(FormTypes::Square, $property->getValue($initialsAvatar));

$initialsAvatar->setForm('hexagon');
$this->assertEquals(FormTypes::Hexagon, $property->getValue($initialsAvatar));

$initialsAvatar->setForm(FormTypes::Circle);
$this->assertEquals(FormTypes::Circle, $property->getValue($initialsAvatar));

$initialsAvatar->setForm(FormTypes::Square);
$this->assertEquals(FormTypes::Square, $property->getValue($initialsAvatar));

$initialsAvatar->setForm(FormTypes::Hexagon);
$this->assertEquals(FormTypes::Hexagon, $property->getValue($initialsAvatar));
}
}
Loading