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