summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/farmer/3rdparty/RingIcon.php
blob: c8dfdbcd585533c6d82f2bfecdca36e7e7c3abbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

namespace splitbrain\RingIcon;

/**
 * Class RingIcon
 *
 * Generates a identicon/visiglyph like image based on concentric rings
 *
 * @todo add a mono color version
 * @author Andreas Gohr <andi@splitbrain.org>
 * @license MIT
 * @package splitbrain\RingIcon
 */
class RingIcon
{

    protected $size;
    protected $fullsize;
    protected $rings;
    protected $center;
    protected $ringwidth;
    protected $seed;
    protected $ismono = false;
    protected $monocolor = null;

    /**
     * RingIcon constructor.
     * @param int $size width and height of the resulting image
     * @param int $rings number of rings
     */
    public function __construct($size, $rings = 3)
    {
        $this->size = $size;
        $this->fullsize = $this->size * 5;
        $this->rings = 4;

        $this->center = floor($this->fullsize / 2);
        $this->ringwidth = floor($this->fullsize / $rings);

        $this->seed = mt_rand() . time();
    }

    /**
     * Generates an ring image
     *
     * If a seed is given, the image will be based on that seed
     *
     * @param string $seed initialize the genrator with this string
     * @param string $file if given, the image is saved at that path, otherwise is printed to browser
     */
    public function createImage($seed = '', $file = '')
    {
        if (!$seed) {
            $seed = mt_rand() . time();
        }
        $this->seed = $seed;

        // monochrome wanted?
        if($this->ismono) {
            $this->monocolor = array(
                $this->rand(20,255),
                $this->rand(20,255),
                $this->rand(20,255)
            );
        } else {
            $this->monocolor = null;
        }

        // create
        $image = $this->createTransparentImage($this->fullsize, $this->fullsize);
        $arcwidth = $this->fullsize;
        for ($i = $this->rings; $i > 0; $i--) {
            $this->drawRing($image, $arcwidth);
            $arcwidth -= $this->ringwidth;
        }

        // resample for antialiasing
        $out = $this->createTransparentImage($this->size, $this->size);
        imagecopyresampled($out, $image, 0, 0, 0, 0, $this->size, $this->size, $this->fullsize, $this->fullsize);
        if ($file) {
            imagepng($out, $file);
        } else {
            header("Content-type: image/png");
            imagepng($out);
        }
        imagedestroy($out);
        imagedestroy($image);
    }

    /**
     * When set to true a monochrome version is returned
     *
     * @param bool $ismono
     */
    public function setMono($ismono) {
        $this->ismono = $ismono;
    }

    /**
     * Generate number from seed
     *
     * Each call runs MD5 on the seed again
     *
     * @param int $min
     * @param int $max
     * @return int
     */
    protected function rand($min, $max)
    {
        $this->seed = md5($this->seed);
        $rand = hexdec(substr($this->seed, 0, 8));
        return ($rand % ($max - $min + 1)) + $min;
    }

    /**
     * Drawas a single ring
     *
     * @param resource $image
     * @param int $arcwidth outer width of the ring
     */
    protected function drawRing($image, $arcwidth)
    {
        $color = $this->randomColor($image);
        $transparency = $this->transparentColor($image);

        $start = $this->rand(20, 360);
        $stop = $this->rand(20, 360);
        if($stop < $start) list($start, $stop) = array($stop, $start);

        imagefilledarc($image, $this->center, $this->center, $arcwidth, $arcwidth, $stop, $start, $color, IMG_ARC_PIE);
        imagefilledellipse($image, $this->center, $this->center, $arcwidth - $this->ringwidth,
            $arcwidth - $this->ringwidth, $transparency);

        imagecolordeallocate($image, $color);
        imagecolordeallocate($image, $transparency);
    }

    /**
     * Allocate a transparent color
     *
     * @param resource $image
     * @return int
     */
    protected function transparentColor($image)
    {
        return imagecolorallocatealpha($image, 0, 0, 0, 127);
    }

    /**
     * Allocate a random color
     *
     * @param $image
     * @return int
     */
    protected function randomColor($image)
    {
        if($this->ismono) {
            return imagecolorallocatealpha($image, $this->monocolor[0], $this->monocolor[1], $this->monocolor[2], $this->rand(0, 96));
        }
        return imagecolorallocate($image, $this->rand(0, 255), $this->rand(0, 255), $this->rand(0, 255));
    }

    /**
     * Create a transparent image
     *
     * @param int $width
     * @param int $height
     * @return resource
     * @throws \Exception
     */
    protected function createTransparentImage($width, $height)
    {
        $image = @imagecreatetruecolor($width, $height);
        if (!$image) {
            throw new \Exception('Missing libgd support');
        }
        imagealphablending($image, false);
        $transparency = $this->transparentColor($image);
        imagefill($image, 0, 0, $transparency);
        imagecolordeallocate($image, $transparency);
        imagesavealpha($image, true);
        return $image;
    }

}