Completed
Push — master ( 759bb9...a57611 )
by Dan
05:46 queued 02:35
created

Drawer::createImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud\Drawer;
4
5
use Imagine\Gd\Imagine;
6
use Imagine\Image\Box;
7
use Imagine\Image\Color;
8
use Imagine\Image\ImageInterface;
9
use Imagine\Image\Point;
10
use SixtyNine\Cloud\Model\Box as MyBox;
11
use SixtyNine\Cloud\Factory\FontsFactory;
12
use SixtyNine\Cloud\Usher\MaskInterface;
13
14
class Drawer
15
{
16
    /** @var \SixtyNine\Cloud\Factory\FontsFactory */
17
    protected $fontsFactory;
18
19
    /** @var \Imagine\Gd\Imagine */
20
    protected $imagine;
21
22
    /** @var ImageInterface */
23
    protected $image;
24
25
    /** @var string */
26
    protected $font;
27
28
    /**
29
     * @param FontsFactory $fontsFactory
30
     */
31 4
    protected function __construct(FontsFactory $fontsFactory)
32
    {
33 4
        $this->imagine = new Imagine();
34 4
        $this->fontsFactory = $fontsFactory;
35 4
    }
36
37
    /**
38
     * @return Drawer
39
     */
40 4
    public static function create(FontsFactory $fontsFactory)
41
    {
42 4
        return new self($fontsFactory);
43
    }
44
45
    /**
46
     * @param int $width
47
     * @param int $height
48
     * @param string $color
49
     * @return Drawer
50
     */
51 4
    public function createImage($width, $height, $color = '#FFFFFF')
52
    {
53 4
        $this->image = $this->imagine->create(
54 4
            new Box($width, $height),
55 4
            new Color($color)
56
        );
57 4
        return $this;
58
    }
59
60
    /**
61
     * @param string $fontName
62
     * @return Drawer
63
     */
64 4
    public function setFont($fontName)
65
    {
66 4
        $this->font = $fontName;
67 4
        return $this;
68
    }
69
70
    /**
71
     * @param int $x
72
     * @param int $y
73
     * @param string $text
74
     * @param int $size
75
     * @param string $color
76
     * @param int $angle
77
     * @throws \InvalidArgumentException
78
     * @return Drawer
79
     */
80 4
    public function drawText($x, $y, $text, $size, $color = '#000000', $angle = 0)
81
    {
82 4
        if (!$this->font) {
83
            throw new \InvalidArgumentException('Font not set');
84
        }
85
86 4
        $font = $this->fontsFactory->getImagineFont($this->font, $size, $color);
87 4
        $this->image->draw()->text($text, $font, new Point($x , $y), $angle);
88
89 4
        return $this;
90
    }
91
92
    /**
93
     * @param int $x
94
     * @param int $y
95
     * @param int $width
96
     * @param int $height
97
     * @param string $color
98
     * @return Drawer
99
     */
100 4
    public function drawBox($x, $y, $width, $height, $color = '#000000')
101
    {
102 4
        if ($x < 0) {
103 1
            $width += $x;
104 1
            $x = 0;
105
        }
106
107 4
        if ($y < 0) {
108 1
            $height += $y;
109 1
            $y = 0;
110
        }
111
112
        $points = array(
113 4
            new Point($x, $y),
114 4
            new Point($x + $width, $y),
115 4
            new Point($x + $width, $y + $height),
116 4
            new Point($x, $y + $height),
117
        );
118
119 4
        $this->image->draw()->polygon($points, new Color($color));
120
121 4
        return $this;
122
    }
123
124
    /**
125
     * @param int $x
126
     * @param int $y
127
     * @param int $width
128
     * @param int $height
129
     * @param int $angle
130
     * @param string $color
131
     * @return Drawer
132
     */
133 2
    public function drawBoxForText($x, $y, $width, $height, $angle, $color = '#000000')
134
    {
135 2
        $box = self::getBoxFoxText($x, $y, $width, $height, $angle);
136 2
        return $this->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color);
137
    }
138
139
    /**
140
     * @param int $x
141
     * @param int $y
142
     * @param int $width
143
     * @param int $height
144
     * @param int $angle
145
     * @return MyBox
146
     */
147 7
    public static function getBoxFoxText($x, $y, $width, $height, $angle)
148
    {
149
        switch ($angle) {
150 7
            case 90:
151 1
                return new MyBox($x, $y + $height, $height, $width);
152 7
            case 180:
153 1
                return new MyBox($x - $width, $y + $height, $width, $height);
154 7
            case 270:
155 5
                return new MyBox($x - $height, $y - $width + $height, $height, $width);
156
        }
157
158 7
        return new MyBox($x, $y, $width, $height);
159
    }
160
161 1
    public function drawMask(MaskInterface $mask, $color = '#ffffff')
162
    {
163 1
        foreach ($mask->getBoxes() as $box) {
164 1
            $this->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color);
165
        }
166 1
        return $this;
167
    }
168
169
    /**
170
     * @return ImageInterface
171
     */
172 4
    public function getImage()
173
    {
174 4
        if (!$this->image) {
175
            throw new \InvalidArgumentException('Image not created');
176
        }
177
178 4
        return $this->image;
179
    }
180
}
181