1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Tile; |
4
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\API\Image\ImageInterface; |
6
|
|
|
use AmaTeam\Image\Projection\API\Tile\PositionInterface; |
7
|
|
|
use AmaTeam\Image\Projection\Image\EncodingOptions; |
8
|
|
|
use AmaTeam\Image\Projection\Image\Manager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Represents single tile - rectangular projection chunk. |
12
|
|
|
*/ |
13
|
|
|
class Tile |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $path; |
19
|
|
|
/** |
20
|
|
|
* @var Manager |
21
|
|
|
*/ |
22
|
|
|
private $manager; |
23
|
|
|
/** |
24
|
|
|
* @var PositionInterface |
25
|
|
|
*/ |
26
|
|
|
private $position; |
27
|
|
|
/** |
28
|
|
|
* @var ImageInterface |
29
|
|
|
*/ |
30
|
|
|
private $image; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $path |
34
|
|
|
* @param PositionInterface $position |
35
|
|
|
* @param Manager $manager |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
$path, |
39
|
|
|
PositionInterface $position, |
40
|
|
|
Manager $manager |
41
|
|
|
) { |
42
|
|
|
$this->path = $path; |
43
|
|
|
$this->manager = $manager; |
44
|
|
|
$this->position = $position; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getImage() |
48
|
|
|
{ |
49
|
|
|
if (!$this->image) { |
50
|
|
|
$this->image = $this->manager->read($this->path); |
51
|
|
|
} |
52
|
|
|
return $this->image; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function createImage($width, $height) |
56
|
|
|
{ |
57
|
|
|
$this->image = $this->manager->create($width, $height); |
58
|
|
|
return $this->image; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function persist($format, EncodingOptions $options = null) |
62
|
|
|
{ |
63
|
|
|
if ($this->image) { |
64
|
|
|
$this->manager->save($this->image, $this->path, $format, $options); |
65
|
|
|
} |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return PositionInterface |
71
|
|
|
*/ |
72
|
|
|
public function getPosition() |
73
|
|
|
{ |
74
|
|
|
return $this->position; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getPath() |
81
|
|
|
{ |
82
|
|
|
return $this->path; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Converts single level tiles array into three level tree |
87
|
|
|
* (face -> rows -> columns). |
88
|
|
|
* |
89
|
|
|
* @param Tile[] $tiles |
90
|
|
|
* @return Tile[][][] |
91
|
|
|
*/ |
92
|
|
|
public static function treeify(array $tiles) |
93
|
|
|
{ |
94
|
|
|
$target = []; |
95
|
|
|
foreach ($tiles as $tile) { |
96
|
|
|
$position = $tile->getPosition(); |
97
|
|
|
$cursor = &$target; |
98
|
|
|
foreach ([$position->getFace(), $position->getY()] as $segment) { |
99
|
|
|
if (!isset($cursor[$segment])) { |
100
|
|
|
$cursor[$segment] = []; |
101
|
|
|
} |
102
|
|
|
$cursor = &$cursor[$segment]; |
103
|
|
|
} |
104
|
|
|
$cursor[$position->getX()] = $tile; |
105
|
|
|
} |
106
|
|
|
return $target; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Tile[][][] $tree |
111
|
|
|
* @return Tile[] |
112
|
|
|
*/ |
113
|
|
|
public static function flatten(array $tree) |
114
|
|
|
{ |
115
|
|
|
$target = []; |
116
|
|
|
foreach ($tree as $face) { |
117
|
|
|
foreach ($face as $row) { |
118
|
|
|
foreach ($row as $tile) { |
119
|
|
|
$target[] = $tile; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $target; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|