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\Drawer\Drawer; |
12
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
13
|
|
|
use SixtyNine\Cloud\Model\Cloud; |
14
|
|
|
use SixtyNine\Cloud\Placer\PlacerInterface; |
15
|
|
|
|
16
|
|
|
class CloudRenderer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param Cloud $cloud |
20
|
|
|
* @param \SixtyNine\Cloud\Factory\FontsFactory $fontsFactory |
21
|
|
|
* @param bool $drawBoundingBoxes |
22
|
|
|
* @return \Imagine\Gd\Image|\Imagine\Image\ImageInterface |
23
|
|
|
*/ |
24
|
1 |
|
public function render(Cloud $cloud, FontsFactory $fontsFactory, $drawBoundingBoxes = false) |
25
|
|
|
{ |
26
|
1 |
|
$drawer = Drawer::create($fontsFactory) |
27
|
1 |
|
->createImage($cloud->getWidth(), $cloud->getHeight(), $cloud->getBackgroundColor()) |
28
|
1 |
|
->setFont($cloud->getFont()) |
29
|
|
|
; |
30
|
|
|
|
31
|
|
|
/** @var \SixtyNine\Cloud\Model\CloudWord $word */ |
32
|
1 |
|
foreach ($cloud->getWords() as $word) { |
33
|
|
|
|
34
|
1 |
|
if (!$word->getIsVisible()) { |
35
|
1 |
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$pos = $word->getPosition(); |
39
|
1 |
|
$box = $word->getBox(); |
40
|
|
|
|
41
|
1 |
|
if ($word->getAngle() === 270) { |
42
|
1 |
|
$drawer->drawText($pos[0] + $box->getWidth(), $pos[1] + $box->getHeight() - $word->getSize(), $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle()); |
43
|
|
|
} else { |
44
|
1 |
|
$drawer->drawText($pos[0], $pos[1], $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle()); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if ($drawBoundingBoxes) { |
48
|
1 |
|
$drawer->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), '#ff0000'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $drawer->getImage(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ImageInterface $image |
57
|
|
|
* @param PlacerInterface $placer |
58
|
|
|
* @param string $color |
59
|
|
|
* @param int $maxIterations |
60
|
|
|
*/ |
61
|
3 |
|
public function renderUsher( |
62
|
|
|
ImageInterface $image, |
63
|
|
|
PlacerInterface $placer, |
64
|
|
|
$color, |
65
|
|
|
$maxIterations = 5000 |
66
|
|
|
) { |
67
|
3 |
|
$i = 0; |
68
|
3 |
|
$cur = $placer->getFirstPlaceToTry(); |
69
|
3 |
|
$color = new Color($color); |
70
|
|
|
|
71
|
3 |
|
while($cur) { |
72
|
|
|
|
73
|
3 |
|
$next = $placer->getNextPlaceToTry($cur); |
74
|
|
|
|
75
|
3 |
|
if ($next) { |
76
|
3 |
|
$image->draw()->line($cur, $next, $color); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$i++; |
80
|
3 |
|
$cur = $next; |
81
|
|
|
|
82
|
3 |
|
if ($i >= $maxIterations) { |
83
|
1 |
|
break; |
84
|
|
|
} |
85
|
|
|
} |
86
|
3 |
|
} |
87
|
|
|
} |
88
|
|
|
|