xref: /plugin/farmer/vendor/splitbrain/php-ringicon/src/RingIcon.php (revision c609f1dcc91a56df760d51ba92f6e25b7289002c)
1<?php
2
3namespace splitbrain\RingIcon;
4
5
6class RingIcon extends AbstractRingIcon
7{
8    /**
9     * Generates an ring image
10     *
11     * If a seed is given, the image will be based on that seed
12     *
13     * @param string $seed initialize the genrator with this string
14     * @param string $file if given, the image is saved at that path, otherwise is printed to browser
15     */
16    public function createImage($seed = '', $file = '')
17    {
18        if (!$seed) {
19            $seed = mt_rand() . time();
20        }
21        $this->seed = $seed;
22
23        // monochrome wanted?
24        if($this->ismono) {
25            $this->generateMonoColor();
26        }
27
28        // create
29        $image = $this->createTransparentImage($this->fullsize, $this->fullsize);
30        $arcwidth = $this->fullsize;
31        for ($i = $this->rings; $i > 0; $i--) {
32            $this->drawRing($image, $arcwidth);
33            $arcwidth -= $this->ringwidth;
34        }
35
36        // resample for antialiasing
37        $out = $this->createTransparentImage($this->size, $this->size);
38        imagecopyresampled($out, $image, 0, 0, 0, 0, $this->size, $this->size, $this->fullsize, $this->fullsize);
39        if ($file) {
40            imagepng($out, $file);
41        } else {
42            header("Content-type: image/png");
43            imagepng($out);
44        }
45        imagedestroy($out);
46        imagedestroy($image);
47    }
48
49
50    /**
51     * Drawas a single ring
52     *
53     * @param resource $image
54     * @param int $arcwidth outer width of the ring
55     */
56    protected function drawRing($image, $arcwidth)
57    {
58        $color = $this->randomColor($image);
59        $transparency = $this->transparentColor($image);
60
61        $start = $this->rand(20, 360);
62        $stop = $this->rand(20, 360);
63        if($stop < $start) list($start, $stop) = array($stop, $start);
64
65        imagefilledarc($image, $this->center, $this->center, $arcwidth, $arcwidth, $stop, $start, $color, IMG_ARC_PIE);
66        imagefilledellipse($image, $this->center, $this->center, $arcwidth - $this->ringwidth,
67            $arcwidth - $this->ringwidth, $transparency);
68
69        imagecolordeallocate($image, $color);
70        imagecolordeallocate($image, $transparency);
71    }
72
73    /**
74     * Allocate a transparent color
75     *
76     * @param resource $image
77     * @return int
78     */
79    protected function transparentColor($image)
80    {
81        return imagecolorallocatealpha($image, 0, 0, 0, 127);
82    }
83
84    /**
85     * Allocate a random color
86     *
87     * @param $image
88     * @return int
89     */
90    protected function randomColor($image)
91    {
92        if($this->ismono) {
93            return imagecolorallocatealpha($image, $this->monocolor[0], $this->monocolor[1], $this->monocolor[2], $this->rand(0, 96));
94        }
95        return imagecolorallocate($image, $this->rand(0, 255), $this->rand(0, 255), $this->rand(0, 255));
96    }
97
98    /**
99     * Create a transparent image
100     *
101     * @param int $width
102     * @param int $height
103     * @return resource
104     * @throws \Exception
105     */
106    protected function createTransparentImage($width, $height)
107    {
108        $image = @imagecreatetruecolor($width, $height);
109        if (!$image) {
110            throw new \Exception('Missing libgd support');
111        }
112        imagealphablending($image, false);
113        $transparency = $this->transparentColor($image);
114        imagefill($image, 0, 0, $transparency);
115        imagecolordeallocate($image, $transparency);
116        imagesavealpha($image, true);
117        return $image;
118    }
119}
120