xref: /plugin/farmer/vendor/splitbrain/php-ringicon/src/AbstractRingIcon.php (revision c609f1dcc91a56df760d51ba92f6e25b7289002c)
1<?php
2
3namespace splitbrain\RingIcon;
4
5/**
6 * Class RingIcon
7 *
8 * Generates a identicon/visiglyph like image based on concentric rings
9 *
10 * @author Andreas Gohr <andi@splitbrain.org>
11 * @license MIT
12 * @package splitbrain\RingIcon
13 */
14abstract class AbstractRingIcon
15{
16
17    protected $size;
18    protected $fullsize;
19    protected $rings;
20    protected $center;
21    protected $ringwidth;
22    protected $seed;
23    protected $ismono = false;
24    protected $monocolor = null;
25
26    /**
27     * RingIcon constructor.
28     * @param int $size width and height of the resulting image
29     * @param int $rings number of rings
30     */
31    public function __construct($size, $rings = 3)
32    {
33        $this->size = $size;
34        $this->fullsize = $this->size * 5;
35        $this->rings = $rings;
36
37        $this->center = floor($this->fullsize / 2);
38        $this->ringwidth = floor($this->fullsize / $rings);
39
40        $this->seed = mt_rand() . time();
41    }
42
43    /**
44     * When set to true a monochrome version is returned
45     *
46     * @param bool $ismono
47     */
48    public function setMono($ismono) {
49        $this->ismono = $ismono;
50    }
51
52    /**
53     * Generate number from seed
54     *
55     * Each call runs MD5 on the seed again
56     *
57     * @param int $min
58     * @param int $max
59     * @return int
60     */
61    protected function rand($min, $max)
62    {
63        $this->seed = md5($this->seed);
64        $rand = hexdec(substr($this->seed, 0, 8));
65        return ($rand % ($max - $min + 1)) + $min;
66    }
67
68    /**
69     * Set a fixed color, which to be later only varied within its alpha channel
70     */
71    protected function generateMonoColor()
72    {
73        $this->monocolor = array(
74            $this->rand(20, 255),
75            $this->rand(20, 255),
76            $this->rand(20, 255)
77        );
78    }
79}
80