1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Renderer; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Imagine\Gd\Imagine; |
7
|
|
|
use Imagine\Image\Box; |
8
|
|
|
use Imagine\Image\Color; |
9
|
|
|
use Imagine\Image\ImageInterface; |
10
|
|
|
use Imagine\Image\Point; |
11
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
12
|
|
|
use SixtyNine\Cloud\Model\Cloud; |
13
|
|
|
use SixtyNine\Cloud\Placer\PlacerInterface; |
14
|
|
|
|
15
|
|
|
class CloudRenderer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Cloud $cloud |
19
|
|
|
* @param bool $drawBoundingBoxes |
20
|
|
|
* @return \Imagine\Gd\Image|\Imagine\Image\ImageInterface |
21
|
|
|
*/ |
22
|
1 |
|
public function render(Cloud $cloud, FontsFactory $fontsFactory, $drawBoundingBoxes = false) |
23
|
|
|
{ |
24
|
1 |
|
$imagine = new Imagine(); |
25
|
1 |
|
$size = new Box($cloud->getWidth(), $cloud->getHeight()); |
26
|
1 |
|
$image = $imagine->create( |
27
|
|
|
$size, |
28
|
1 |
|
new Color($cloud->getBackgroundColor()) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** @var \SixtyNine\Cloud\Model\CloudWord $word */ |
32
|
1 |
|
foreach ($cloud->getWords() as $word) { |
33
|
|
|
|
34
|
1 |
|
if (!$word->getIsVisible()) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$font = $fontsFactory->getImagineFont($cloud->getFont(), $word->getSize(), $word->getColor()); |
39
|
1 |
|
$angle = $word->getAngle(); |
40
|
1 |
|
$pos = $word->getPosition(); |
41
|
1 |
|
$box =$word->getBox(); |
42
|
|
|
|
43
|
1 |
|
if ($angle === 0) { |
44
|
1 |
|
$image->draw()->text( |
45
|
1 |
|
$word->getText(), |
46
|
|
|
$font, |
47
|
1 |
|
new Point($pos[0], $pos[1]), |
48
|
|
|
$angle |
49
|
|
|
); |
50
|
|
|
} else { |
51
|
1 |
|
$image->draw()->text( |
52
|
1 |
|
$word->getText(), |
53
|
|
|
$font, |
54
|
1 |
|
new Point($pos[0], $pos[1] - $box[0]), |
55
|
|
|
$angle |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if ($drawBoundingBoxes) { |
60
|
1 |
|
if ($word->getAngle() === 0) { |
61
|
|
|
$points = array( |
62
|
1 |
|
new Point($pos[0], $pos[1]), |
63
|
1 |
|
new Point($pos[0] + $box[0], $pos[1]), |
64
|
1 |
|
new Point($pos[0] + $box[0], $pos[1] + $box[1]), |
65
|
1 |
|
new Point($pos[0], $pos[1] + $box[1]), |
66
|
|
|
); |
67
|
|
|
} else { |
68
|
|
|
$points = array( |
69
|
1 |
|
new Point($pos[0], $pos[1]), |
70
|
1 |
|
new Point($pos[0] - $box[0], $pos[1]), |
71
|
1 |
|
new Point($pos[0] - $box[0], $pos[1] - $box[1]), |
72
|
1 |
|
new Point($pos[0], $pos[1] - $box[1]), |
73
|
|
|
); |
74
|
|
|
} |
75
|
1 |
|
$image->draw()->polygon($points, new Color(0xFF0000)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $image; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function renderUsher( |
83
|
|
|
ImageInterface $image, |
84
|
|
|
PlacerInterface $placer, |
85
|
|
|
$color, |
86
|
|
|
$maxIterations = 5000 |
87
|
|
|
) { |
88
|
1 |
|
$i = 0; |
89
|
1 |
|
$cur = $placer->getFirstPlaceToTry(); |
90
|
1 |
|
$color = new Color($color); |
91
|
|
|
|
92
|
1 |
|
while($cur) { |
93
|
|
|
|
94
|
1 |
|
$next = $placer->getNextPlaceToTry($cur); |
95
|
|
|
|
96
|
1 |
|
if ($next) { |
97
|
1 |
|
$image->draw()->line($cur, $next, $color); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$i++; |
101
|
1 |
|
$cur = $next; |
102
|
|
|
|
103
|
1 |
|
if ($i >= $maxIterations) { |
104
|
1 |
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
} |
108
|
|
|
} |
109
|
|
|
|