1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Tests\Builder; |
4
|
|
|
|
5
|
|
|
use Imagine\Gd\Imagine; |
6
|
|
|
use Imagine\Image\Box as ImagineBox; |
7
|
|
|
use Imagine\Image\Color; |
8
|
|
|
use Imagine\Image\ImageInterface; |
9
|
|
|
use Imagine\Image\Point; |
10
|
|
|
use Imagine\Image\PointInterface; |
11
|
|
|
use SixtyNine\Cloud\Drawer\Drawer; |
12
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
13
|
|
|
use SixtyNine\Cloud\Factory\PlacerFactory; |
14
|
|
|
use SixtyNine\Cloud\FontMetrics; |
15
|
|
|
use SixtyNine\Cloud\Renderer\CloudRenderer; |
16
|
|
|
use SixtyNine\Cloud\Usher\Usher; |
17
|
|
|
|
18
|
|
|
class DrawingTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Illustrate the difficulty to properly draw a bounding box around rotated text with Imagine. |
22
|
|
|
*/ |
23
|
|
|
public function testDrawing() |
24
|
|
|
{ |
25
|
|
|
$imagine = new Imagine(); |
26
|
|
|
$image = $imagine->create(new ImagineBox(400, 400), new Color('#000000')); |
27
|
|
|
|
28
|
|
|
$text = 'Foobar'; |
29
|
|
|
$size = 80; |
30
|
|
|
|
31
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
32
|
|
|
$metrics = new FontMetrics($factory); |
33
|
|
|
$font = $factory->getImagineFont('Arial.ttf', $size, '#ffffff'); |
34
|
|
|
|
35
|
|
|
$fontSize = $metrics->calculateSize($text, 'Arial.ttf', $size, 0); |
36
|
|
|
$pos = array(200, 200); |
37
|
|
|
$box = array($fontSize->getWidth(), $fontSize->getHeight()); |
38
|
|
|
|
39
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 0); |
40
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 90); |
41
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 180); |
42
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 270); |
43
|
|
|
|
44
|
|
|
// Angle = 0 - so far so good... |
45
|
|
|
$this->drawBox($image, $this->createPoint($pos[0], $pos[1]), $box[0], $box[1], 0xFF0000); |
46
|
|
|
|
47
|
|
|
// Angle = 90 - Y needs to be adjusted, dimensions inverted |
48
|
|
|
$this->drawBox($image, $this->createPoint($pos[0], $pos[1] + $box[1]), $box[1], $box[0], 0xFF0000); |
49
|
|
|
|
50
|
|
|
// Angle = 180 - X and Y need adjustments, X might go out of the image on the left (which Imagine does not like) |
51
|
|
|
$x = $pos[0] - $box[0]; |
52
|
|
|
$width = $x >= 0 ? $box[0] : $box[0] + $x; |
53
|
|
|
$this->drawBox($image, $this->createPoint($x, $pos[1] + $box[1]), $width, $box[1], 0xFF0000); |
54
|
|
|
|
55
|
|
|
// Angle = 270 - X and Y need adjustments, it depends on the font size, Y might go out of the image on the top, dimensions inverted |
56
|
|
|
$y = $pos[1] - $box[0] + $size; |
57
|
|
|
$height = $y >= 0 ? $box[0] : $box[0] + $y; |
58
|
|
|
$this->drawBox($image, $this->createPoint($pos[0] - $box[1], $y), $box[1], $height, 0xFF0000); |
59
|
|
|
|
60
|
|
|
$image->save('/tmp/test1.png'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Now the same using the Drawer class. |
65
|
|
|
*/ |
66
|
|
|
public function testDrawingWithDrawer() |
67
|
|
|
{ |
68
|
|
|
$text = 'Foobar'; |
69
|
|
|
$size = 80; |
70
|
|
|
|
71
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
72
|
|
|
$metrics = new FontMetrics($factory); |
73
|
|
|
$fontSize = $metrics->calculateSize($text, 'Arial.ttf', $size, 0); |
74
|
|
|
$pos = array(200, 200); |
75
|
|
|
$box = array($fontSize->getWidth(), $fontSize->getHeight()); |
76
|
|
|
|
77
|
|
|
$image = Drawer::create($factory) |
78
|
|
|
->createImage(400, 400, '#000000') |
79
|
|
|
->setFont('Arial.ttf') |
80
|
|
|
->drawText($pos[0], $pos[1], $text, $size, '#FFFFFF', 0) |
81
|
|
|
->drawText($pos[0], $pos[1], $text, $size, '#FFFFFF', 90) |
82
|
|
|
->drawText($pos[0], $pos[1], $text, $size, '#FFFFFF', 180) |
83
|
|
|
->drawText($pos[0], $pos[1], $text, $size, '#FFFFFF', 270) |
84
|
|
|
->drawBoxForText($pos[0], $pos[1], $box[0], $box[1], 0, '#00ffff') |
85
|
|
|
->drawBoxForText($pos[0], $pos[1], $box[0], $box[1], 90, '#00ffff') |
86
|
|
|
->drawBoxForText($pos[0], $pos[1], $box[0], $box[1], 180, '#00ffff') |
87
|
|
|
->drawBoxForText($pos[0], $pos[1], $box[0], $box[1], 270, '#00ffff') |
88
|
|
|
->getImage() |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
$image->save('/tmp/test2.png'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testDrawingAtOrigin() |
95
|
|
|
{ |
96
|
|
|
$text = 'Foobar'; |
97
|
|
|
$size = 80; |
98
|
|
|
|
99
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
100
|
|
|
$metrics = new FontMetrics($factory); |
101
|
|
|
$fontSize = $metrics->calculateSize($text, 'Arial.ttf', $size, 0); |
102
|
|
|
$pos = array(0, 0); |
103
|
|
|
$box = array($fontSize->getWidth(), $fontSize->getHeight()); |
104
|
|
|
|
105
|
|
|
$image = Drawer::create($factory) |
106
|
|
|
->createImage(400, 400, '#000000') |
107
|
|
|
->setFont('Arial.ttf') |
108
|
|
|
->drawText($pos[0], $pos[1], $text, $size, '#FFFFFF', 0) |
109
|
|
|
->drawBoxForText($pos[0], $pos[1], $box[0], $box[1], 0, '#00ffff') |
110
|
|
|
->getImage() |
111
|
|
|
; |
112
|
|
|
|
113
|
|
|
$image->save('/tmp/test3.png'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testDrawingWithUsher() |
117
|
|
|
{ |
118
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
119
|
|
|
$metrics = new FontMetrics($factory); |
120
|
|
|
$placer = PlacerFactory::getInstance()->getPlacer(PlacerFactory::PLACER_CIRCULAR, 400, 400, 5); |
121
|
|
|
$usher = new Usher(400, 400, $placer, $metrics); |
122
|
|
|
$drawer = Drawer::create($factory) |
123
|
|
|
->createImage(400, 400, '#000000') |
124
|
|
|
->setFont('Arial.ttf') |
125
|
|
|
; |
126
|
|
|
|
127
|
|
|
(new CloudRenderer())->renderUsher($drawer->getImage(), $placer, '#202020'); |
128
|
|
|
|
129
|
|
|
$words = array( |
130
|
|
|
array('first', 50, 0, '#ff0000'), |
131
|
|
|
array('second', 20, 270, '#0000ff'), |
132
|
|
|
array('third', 24, 0, '#00ff00'), |
133
|
|
|
array('fourth', 36, 270, '#00ffff'), |
134
|
|
|
array('fifth', 36, 0, '#ff00ff'), |
135
|
|
|
array('sixth', 20, 0, '#ffff00'), |
136
|
|
|
array('seventh', 24, 270, '#aaaaaa'), |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
foreach ($words as $values) { |
140
|
|
|
$place = $usher->getPlace($values[0], 'Arial.ttf', $values[1], $values[2]); |
141
|
|
|
if ($place) { |
142
|
|
|
if ($values[2] === 0) { |
143
|
|
|
$drawer->drawText($place->getX(), $place->getY(), $values[0], $values[1], $values[3], $values[2]); |
144
|
|
|
} else { |
145
|
|
|
$drawer->drawText($place->getX() + $place->getWidth(), $place->getY() + $place->getHeight() - $values[1], $values[0], $values[1], $values[3], $values[2]); |
146
|
|
|
} |
147
|
|
|
$drawer->drawBox($place->getX(), $place->getY(), $place->getWidth(), $place->getHeight(), $values[3]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$drawer->drawMask($usher->getMask(), '#ffffff'); |
152
|
|
|
|
153
|
|
|
$drawer->getImage()->save('/tmp/test4.png'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testDrawingWithPreciseUsher() |
157
|
|
|
{ |
158
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
159
|
|
|
$metrics = new FontMetrics($factory); |
160
|
|
|
$placer = PlacerFactory::getInstance()->getPlacer(PlacerFactory::PLACER_CIRCULAR, 400, 400, 5); |
161
|
|
|
$usher = new Usher(400, 400, $placer, $metrics); |
162
|
|
|
$drawer = Drawer::create($factory) |
163
|
|
|
->createImage(400, 400, '#000000') |
164
|
|
|
->setFont('Arial.ttf') |
165
|
|
|
; |
166
|
|
|
|
167
|
|
|
(new CloudRenderer())->renderUsher($drawer->getImage(), $placer, '#202020'); |
168
|
|
|
|
169
|
|
|
$words = array( |
170
|
|
|
array('first', 50, 0, '#ff0000'), |
171
|
|
|
array('second', 20, 270, '#0000ff'), |
172
|
|
|
array('third', 24, 0, '#00ff00'), |
173
|
|
|
array('fourth', 36, 270, '#00ffff'), |
174
|
|
|
array('fifth', 36, 0, '#ff00ff'), |
175
|
|
|
array('sixth', 40, 0, '#ffff00'), |
176
|
|
|
array('seventh', 24, 270, '#aaaaaa'), |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
foreach ($words as $values) { |
180
|
|
|
$place = $usher->getPlace($values[0], 'Arial.ttf', $values[1], $values[2], true); |
181
|
|
|
if ($place) { |
182
|
|
|
if ($values[2] === 0) { |
183
|
|
|
$drawer->drawText($place->getX(), $place->getY(), $values[0], $values[1], $values[3], $values[2]); |
184
|
|
|
} else { |
185
|
|
|
$drawer->drawText($place->getX() + $place->getWidth(), $place->getY() + $place->getHeight() - $values[1], $values[0], $values[1], $values[3], $values[2]); |
186
|
|
|
} |
187
|
|
|
$drawer->drawBox($place->getX(), $place->getY(), $place->getWidth(), $place->getHeight(), $values[3]); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$drawer->drawMask($usher->getMask(), '#ffffff'); |
192
|
|
|
|
193
|
|
|
$drawer->getImage()->save('/tmp/test5.png'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function drawBox(ImageInterface $image, PointInterface $pos, $width, $height, $color = '#ffffff') |
197
|
|
|
{ |
198
|
|
|
$x = $pos->getX(); |
199
|
|
|
$y = $pos->getY(); |
200
|
|
|
|
201
|
|
|
$points = array( |
202
|
|
|
$this->createPoint($x, $y), |
203
|
|
|
$this->createPoint($x + $width, $y), |
204
|
|
|
$this->createPoint($x + $width, $y + $height), |
205
|
|
|
$this->createPoint($x, $y + $height), |
206
|
|
|
); |
207
|
|
|
$image->draw()->polygon($points, new Color($color)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function createPoint($x, $y) |
211
|
|
|
{ |
212
|
|
|
$x = $x >= 0 ? $x : 0; |
213
|
|
|
$y = $y >= 0 ? $y : 0; |
214
|
|
|
return new Point($x, $y); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|