Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
86 / 86 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
| SvgAvatarFileFaker | |
100.00% |
86 / 86 |
|
100.00% |
12 / 12 |
38 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSvg | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
5 | |||
| hairBack | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| body | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| hairFront | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
8 | |||
| eyebrows | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| eyes | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| mouth | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
| glasses | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieImageFileFaker; |
| 5 | use Faker\Generator; |
| 6 | use Nyholm\Psr7\Stream; |
| 7 | |
| 8 | /** |
| 9 | * Generates a cartoony avatar as svg: a random combination of skin, hair, eyes, mouth, glasses, beard and clothes. |
| 10 | */ |
| 11 | final class SvgAvatarFileFaker implements ApieImageFileFaker |
| 12 | { |
| 13 | private const BACKGROUNDS = ['#FFD6A5', '#CAFFBF', '#9BF6FF', '#A0C4FF', '#BDB2FF', '#FFC6FF', '#FDFFB6', '#FFADAD', '#B8E0D2', '#E4C1F9']; |
| 14 | private const SKIN_TONES = ['#FFDBB4', '#F1C27D', '#E0AC69', '#C68642', '#8D5524', '#6B4226', '#FAD7C0']; |
| 15 | private const HAIR_COLORS = ['#1B1B1B', '#4A312C', '#8B5A2B', '#D6A64F', '#B55239', '#E8E1CC', '#3B5BDB', '#C2255C', '#2F9E44']; |
| 16 | private const CLOTHES = ['#E63946', '#457B9D', '#2A9D8F', '#F4A261', '#6D597A', '#264653', '#F72585', '#4361EE', '#FFFFFF']; |
| 17 | private const HAIR_STYLES = ['bald', 'short', 'long', 'bun', 'curly', 'mohawk']; |
| 18 | private const EYE_STYLES = ['dot', 'big', 'happy', 'sleepy', 'wink']; |
| 19 | private const MOUTH_STYLES = ['smile', 'open', 'flat', 'smirk', 'tongue']; |
| 20 | |
| 21 | public function createOriginalFilename(Generator $faker): string |
| 22 | { |
| 23 | return $faker->word() . '.svg'; |
| 24 | } |
| 25 | |
| 26 | public function createMimeType(): string |
| 27 | { |
| 28 | return 'image/svg+xml'; |
| 29 | } |
| 30 | |
| 31 | public static function isSupported(): bool |
| 32 | { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | /** @return resource */ |
| 37 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 38 | { |
| 39 | return Stream::create($this->createSvg($faker))->detach(); |
| 40 | } |
| 41 | |
| 42 | private function createSvg(Generator $faker): string |
| 43 | { |
| 44 | $skin = $faker->randomElement(self::SKIN_TONES); |
| 45 | $hairColor = $faker->randomElement(self::HAIR_COLORS); |
| 46 | $hairStyle = $faker->randomElement(self::HAIR_STYLES); |
| 47 | $clothes = $faker->randomElement(self::CLOTHES); |
| 48 | $background = $faker->randomElement(self::BACKGROUNDS); |
| 49 | $clip = $faker->boolean(70) |
| 50 | ? '<circle cx="128" cy="128" r="128"/>' |
| 51 | : '<rect width="256" height="256" rx="' . $faker->numberBetween(0, 64) . '"/>'; |
| 52 | |
| 53 | $svg = '<?xml version="1.0" encoding="UTF-8"?>' |
| 54 | . '<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">' |
| 55 | . '<defs><clipPath id="avatar">' . $clip . '</clipPath></defs>' |
| 56 | . '<g clip-path="url(#avatar)">' |
| 57 | . '<rect width="256" height="256" fill="' . $background . '"/>'; |
| 58 | $svg .= $this->hairBack($hairStyle, $hairColor); |
| 59 | $svg .= $this->body($faker, $skin, $clothes); |
| 60 | $svg .= '<ellipse cx="72" cy="124" rx="9" ry="14" fill="' . $skin . '"/>' |
| 61 | . '<ellipse cx="184" cy="124" rx="9" ry="14" fill="' . $skin . '"/>' |
| 62 | . '<ellipse cx="128" cy="118" rx="52" ry="58" fill="' . $skin . '"/>'; |
| 63 | if ($faker->boolean(25)) { |
| 64 | $svg .= '<path d="M82 132 C 90 190 166 190 174 132 C 166 154 148 160 128 160 C 108 160 90 154 82 132 Z" fill="' . $hairColor . '"/>'; |
| 65 | } |
| 66 | $svg .= $this->hairFront($hairStyle, $hairColor); |
| 67 | $svg .= $this->eyebrows($faker, $hairColor); |
| 68 | $svg .= $this->eyes($faker->randomElement(self::EYE_STYLES)); |
| 69 | // nose |
| 70 | $svg .= '<path d="M124 128 Q 120 142 128 143 Q 134 143 132 138" fill="none" stroke="#00000040" stroke-width="3" stroke-linecap="round"/>'; |
| 71 | if ($faker->boolean(40)) { |
| 72 | $svg .= '<circle cx="94" cy="142" r="9" fill="#FF6B6B" opacity="0.3"/><circle cx="162" cy="142" r="9" fill="#FF6B6B" opacity="0.3"/>'; |
| 73 | } |
| 74 | $svg .= $this->mouth($faker->randomElement(self::MOUTH_STYLES)); |
| 75 | if ($faker->boolean(35)) { |
| 76 | $svg .= $this->glasses($faker->randomElement(self::CLOTHES)); |
| 77 | } |
| 78 | |
| 79 | return $svg . '</g></svg>'; |
| 80 | } |
| 81 | |
| 82 | private function hairBack(string $style, string $color): string |
| 83 | { |
| 84 | return match ($style) { |
| 85 | 'long' => '<path d="M68 118 C 54 30 202 30 188 118 L 200 216 L 56 216 Z" fill="' . $color . '"/>', |
| 86 | 'bun' => '<circle cx="128" cy="34" r="24" fill="' . $color . '"/>', |
| 87 | default => '', |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | private function body(Generator $faker, string $skin, string $clothes): string |
| 92 | { |
| 93 | $svg = '<rect x="108" y="152" width="40" height="44" fill="' . $skin . '"/>' |
| 94 | . '<path d="M30 256 C 30 204 78 184 128 184 C 178 184 226 204 226 256 Z" fill="' . $clothes . '"/>'; |
| 95 | // collar: either a v-neck or a round neckline |
| 96 | $svg .= $faker->boolean() |
| 97 | ? '<path d="M104 186 L 128 218 L 152 186 Z" fill="' . $skin . '"/>' |
| 98 | : '<path d="M104 186 Q 128 212 152 186 Z" fill="' . $skin . '"/>'; |
| 99 | |
| 100 | return $svg; |
| 101 | } |
| 102 | |
| 103 | private function hairFront(string $style, string $color): string |
| 104 | { |
| 105 | $cap = '<path d="M72 112 C 66 50 108 34 128 34 C 150 34 192 50 184 112 C 176 84 154 76 128 76 C 102 76 80 84 72 112 Z" fill="' . $color . '"/>'; |
| 106 | switch ($style) { |
| 107 | case 'short': |
| 108 | case 'long': |
| 109 | case 'bun': |
| 110 | return $cap; |
| 111 | case 'mohawk': |
| 112 | return '<path d="M104 70 C 100 30 112 10 128 6 C 144 10 156 30 152 70 Z" fill="' . $color . '"/>'; |
| 113 | case 'curly': |
| 114 | $svg = ''; |
| 115 | foreach ([[84, 70], [102, 54], [128, 48], [154, 54], [172, 70], [78, 94], [178, 94]] as [$x, $y]) { |
| 116 | $svg .= '<circle cx="' . $x . '" cy="' . $y . '" r="20" fill="' . $color . '"/>'; |
| 117 | } |
| 118 | return $svg; |
| 119 | default: |
| 120 | return ''; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private function eyebrows(Generator $faker, string $color): string |
| 125 | { |
| 126 | $tilt = $faker->numberBetween(-6, 6); |
| 127 | return '<path d="M94 ' . (100 + $tilt) . ' L 120 ' . (100 - $tilt) . '" stroke="' . $color . '" stroke-width="5" stroke-linecap="round"/>' |
| 128 | . '<path d="M136 ' . (100 - $tilt) . ' L 162 ' . (100 + $tilt) . '" stroke="' . $color . '" stroke-width="5" stroke-linecap="round"/>'; |
| 129 | } |
| 130 | |
| 131 | private function eyes(string $style): string |
| 132 | { |
| 133 | $dark = '#222222'; |
| 134 | $arc = fn (int $x): string => '<path d="M' . ($x - 11) . ' 120 Q ' . $x . ' 106 ' . ($x + 11) . ' 120" fill="none" stroke="' . $dark . '" stroke-width="4" stroke-linecap="round"/>'; |
| 135 | $line = fn (int $x): string => '<path d="M' . ($x - 10) . ' 118 L ' . ($x + 10) . ' 118" stroke="' . $dark . '" stroke-width="4" stroke-linecap="round"/>'; |
| 136 | $dot = fn (int $x): string => '<circle cx="' . $x . '" cy="116" r="6" fill="' . $dark . '"/><circle cx="' . ($x + 2) . '" cy="114" r="2" fill="#FFFFFF"/>'; |
| 137 | $big = fn (int $x): string => '<ellipse cx="' . $x . '" cy="116" rx="13" ry="15" fill="#FFFFFF" stroke="' . $dark . '" stroke-width="2"/>' |
| 138 | . '<circle cx="' . $x . '" cy="118" r="7" fill="' . $dark . '"/><circle cx="' . ($x + 2) . '" cy="115" r="2.5" fill="#FFFFFF"/>'; |
| 139 | |
| 140 | return match ($style) { |
| 141 | 'big' => $big(106) . $big(150), |
| 142 | 'happy' => $arc(106) . $arc(150), |
| 143 | 'sleepy' => $line(106) . $line(150), |
| 144 | 'wink' => $dot(106) . $arc(150), |
| 145 | default => $dot(106) . $dot(150), |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | private function mouth(string $style): string |
| 150 | { |
| 151 | return match ($style) { |
| 152 | 'open' => '<path d="M108 152 Q 128 186 148 152 Z" fill="#7A1F2B" stroke="#222222" stroke-width="3" stroke-linejoin="round"/>', |
| 153 | 'flat' => '<path d="M112 158 L 144 158" stroke="#222222" stroke-width="4" stroke-linecap="round"/>', |
| 154 | 'smirk' => '<path d="M110 158 Q 130 162 148 148" fill="none" stroke="#222222" stroke-width="4" stroke-linecap="round"/>', |
| 155 | 'tongue' => '<path d="M108 152 Q 128 176 148 152 Z" fill="#7A1F2B" stroke="#222222" stroke-width="3" stroke-linejoin="round"/>' |
| 156 | . '<path d="M120 164 Q 128 182 138 164 Z" fill="#FF7B9C"/>', |
| 157 | default => '<path d="M108 150 Q 128 172 148 150" fill="none" stroke="#222222" stroke-width="4" stroke-linecap="round"/>', |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | private function glasses(string $color): string |
| 162 | { |
| 163 | $stroke = $color === '#FFFFFF' ? '#222222' : $color; |
| 164 | return '<g fill="#FFFFFF" fill-opacity="0.25" stroke="' . $stroke . '" stroke-width="4">' |
| 165 | . '<circle cx="106" cy="118" r="19"/><circle cx="150" cy="118" r="19"/></g>' |
| 166 | . '<path d="M125 116 L 131 116" stroke="' . $stroke . '" stroke-width="4"/>'; |
| 167 | } |
| 168 | } |