1
0
Fork 0
mirror of https://github.com/Findus23/plugin-ProfileAvatar.git synced 2024-09-19 15:23:45 +02:00
plugin-ProfileAvatar/Generators/Identicon.php
2020-05-20 10:41:04 +02:00

75 lines
2.3 KiB
PHP

<?php
namespace Piwik\Plugins\ProfileAvatar\Generators;
/**
* PHP port of https://github.com/stewartlord/identicon.js
* Copyright (c) 2018, Stewart Lord
* BSD 2-Clause
*/
class Identicon extends PixelBased
{
private $margin = 0.08;
private $saturation = 0.7;
private $lightness = 0.5;
protected function createPatterns(): void
{
$baseMargin = floor($this->size * $this->margin);
$cell = floor(($this->size - ($baseMargin * 2)) / 5);
$margin = floor(($this->size - $cell * 5) / 2);
for ($i = 0; $i < 15; $i++) {
$color = hexdec($this->seed[$i]) % 2 ? $this->backgroundColor : $this->getForegroundColor();
if ($i < 5) {
$this->rectangles[] = [
"x" => 2 * $cell + $margin,
"y" => $i * $cell + $margin,
"w" => $cell,
"h" => $cell,
"color" => $color
];
} elseif ($i < 10) {
$this->rectangles[] = [
"x" => 1 * $cell + $margin,
"y" => ($i - 5) * $cell + $margin,
"w" => $cell,
"h" => $cell,
"color" => $color
];
$this->rectangles[] = [
"x" => 3 * $cell + $margin,
"y" => ($i - 5) * $cell + $margin,
"w" => $cell,
"h" => $cell,
"color" => $color
];
} elseif ($i < 15) {
$this->rectangles[] = [
"x" => 0 * $cell + $margin,
"y" => ($i - 10) * $cell + $margin,
"w" => $cell,
"h" => $cell,
"color" => $color
];
$this->rectangles[] = [
"x" => 4 * $cell + $margin,
"y" => ($i - 10) * $cell + $margin,
"w" => $cell,
"h" => $cell,
"color" => $color
];
}
}
}
protected function getForegroundColor(): array
{
$hue = hexdec(substr($this->seed, -7)) / 0xfffffff;
return [$hue * 360, $this->saturation, $this->lightness];
}
}