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\DataTypes\Box as MyBox; |
11
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
12
|
|
|
use SixtyNine\Cloud\Usher\MaskInterface; |
13
|
|
|
use Webmozart\Assert\Assert; |
14
|
|
|
|
15
|
|
|
class Drawer |
16
|
|
|
{ |
17
|
|
|
/** @var \SixtyNine\Cloud\Factory\FontsFactory */ |
18
|
|
|
protected $fontsFactory; |
19
|
|
|
|
20
|
|
|
/** @var \Imagine\Gd\Imagine */ |
21
|
|
|
protected $imagine; |
22
|
|
|
|
23
|
|
|
/** @var ImageInterface */ |
24
|
|
|
protected $image; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $font; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param FontsFactory $fontsFactory |
31
|
|
|
*/ |
32
|
5 |
|
protected function __construct(FontsFactory $fontsFactory) |
33
|
|
|
{ |
34
|
5 |
|
$this->imagine = new Imagine(); |
35
|
5 |
|
$this->fontsFactory = $fontsFactory; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \SixtyNine\Cloud\Factory\FontsFactory $fontsFactory |
40
|
|
|
* @return Drawer |
41
|
|
|
*/ |
42
|
5 |
|
public static function create(FontsFactory $fontsFactory) |
43
|
|
|
{ |
44
|
5 |
|
return new self($fontsFactory); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int $width |
49
|
|
|
* @param int $height |
50
|
|
|
* @param string $color |
51
|
|
|
* @param int $opacity |
52
|
|
|
* @return Drawer |
53
|
|
|
*/ |
54
|
5 |
|
public function createImage($width, $height, $color = '#FFFFFF', $opacity = 100) |
55
|
|
|
{ |
56
|
5 |
|
$this->image = $this->imagine->create( |
57
|
5 |
|
new Box($width, $height), |
58
|
5 |
|
new Color($color, abs($opacity - 100)) |
59
|
|
|
); |
60
|
5 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $fontName |
65
|
|
|
* @return Drawer |
66
|
|
|
*/ |
67
|
5 |
|
public function setFont($fontName) |
68
|
|
|
{ |
69
|
5 |
|
$this->font = $fontName; |
70
|
5 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $x |
75
|
|
|
* @param int $y |
76
|
|
|
* @param string $text |
77
|
|
|
* @param int $size |
78
|
|
|
* @param string $color |
79
|
|
|
* @param int $angle |
80
|
|
|
* @throws \InvalidArgumentException |
81
|
|
|
* @return Drawer |
82
|
|
|
*/ |
83
|
5 |
|
public function drawText($x, $y, $text, $size, $color = '#000000', $angle = 0) |
84
|
|
|
{ |
85
|
5 |
|
Assert::notNull($this->font, 'Font not set'); |
86
|
5 |
|
$font = $this->fontsFactory->getImagineFont($this->font, $size, $color); |
87
|
5 |
|
$this->image->draw()->text($text, $font, new Point($x , $y), $angle); |
88
|
|
|
|
89
|
5 |
|
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::getBoxForText($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
|
8 |
|
public static function getBoxForText($x, $y, $width, $height, $angle) |
148
|
|
|
{ |
149
|
|
|
switch ($angle) { |
150
|
8 |
|
case 90: |
151
|
1 |
|
return new MyBox($x, $y + $height, $height, $width); |
152
|
8 |
|
case 180: |
153
|
1 |
|
return new MyBox($x - $width, $y + $height, $width, $height); |
154
|
8 |
|
case 270: |
155
|
6 |
|
return new MyBox($x - $height, $y - $width + $height, $height, $width); |
156
|
|
|
} |
157
|
|
|
|
158
|
8 |
|
return new MyBox($x, $y, $width, $height); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param MaskInterface $mask |
163
|
|
|
* @param string $color |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
2 |
|
public function drawMask(MaskInterface $mask, $color = '#ffffff') |
167
|
|
|
{ |
168
|
2 |
|
foreach ($mask->getBoxes() as $box) { |
169
|
2 |
|
$this->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color); |
170
|
|
|
} |
171
|
2 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return ImageInterface |
176
|
|
|
*/ |
177
|
5 |
|
public function getImage() |
178
|
|
|
{ |
179
|
5 |
|
Assert::notNull($this->image, 'Image not created'); |
180
|
5 |
|
return $this->image; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|