1
|
|
|
<?php |
2
|
|
|
/** Created by Gorlum 08.01.2024 19:57 */ |
3
|
|
|
|
4
|
|
|
namespace Tools; |
5
|
|
|
|
6
|
|
|
require_once __DIR__ . '/SpriteLine.php'; |
7
|
|
|
require_once __DIR__ . '/ImageFile.php'; |
8
|
|
|
require_once __DIR__ . '/ImageContainer.php'; |
9
|
|
|
|
10
|
|
|
use ImageFile; |
11
|
|
|
|
12
|
|
|
class Sprite { |
13
|
|
|
const LAYOUT_LINE = 0; |
14
|
|
|
const LAYOUT_COLUMN = 1; |
15
|
|
|
const LAYOUT_SQUARE = 'square'; |
16
|
|
|
const LAYOUT_BTREE = 'btree'; |
17
|
|
|
|
18
|
|
|
/** @var SpriteLine[] $lines */ |
19
|
|
|
public $lines = []; |
20
|
|
|
|
21
|
|
|
protected $gridSize = 0; |
22
|
|
|
|
23
|
|
|
protected $lineIndex = 0; |
24
|
|
|
protected $columnIndex = 0; |
25
|
|
|
|
26
|
|
|
protected $height = 0; |
27
|
|
|
protected $width = 0; |
28
|
|
|
|
29
|
|
|
/** @var ImageContainer|null $image */ |
30
|
|
|
public $image = null; |
31
|
|
|
|
32
|
|
|
public function __construct($gridSize = 0) { |
33
|
|
|
if ($gridSize < 1) { |
34
|
|
|
$gridSize = 1; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->gridSize = $gridSize; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ImageFile[] $images |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
* |
45
|
|
|
* @noinspection PhpUnusedParameterInspection |
46
|
|
|
*/ |
47
|
|
|
public static function createGridSquare($images, $layout) { |
48
|
|
|
$gridSize = ceil(sqrt(count($images))); |
49
|
|
|
usort($images, function (ImageFile $a, ImageFile $b) { return $b->height - $a->height; }); |
50
|
|
|
|
51
|
|
|
$sprite = new static($gridSize); |
52
|
|
|
foreach ($images as $image) { |
53
|
|
|
$sprite->addToGrid($image); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $sprite; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ImageFile $imageFile |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function addToGrid($imageFile) { |
66
|
|
|
// This is first image in row |
67
|
|
|
if (empty($this->lines[$this->lineIndex])) { |
68
|
|
|
$this->lines[$this->lineIndex] = new SpriteLine(); |
69
|
|
|
} |
70
|
|
|
$this->lines[$this->lineIndex]->addImage($imageFile); |
71
|
|
|
if ($this->lines[$this->lineIndex]->getImageCount() >= $this->gridSize) { |
72
|
|
|
$this->lineIndex++; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function imageReset() { |
80
|
|
|
if (!empty($this->image)) { |
81
|
|
|
unset($this->image); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->image = ImageContainer::create($this->width, $this->height); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $scaleToPx |
89
|
|
|
*/ |
90
|
|
|
public function renderLines($scaleToPx) { |
91
|
|
|
$this->width = $this->height = 0; |
92
|
|
|
// Generating lines and calculating line sizes |
93
|
|
|
foreach ($this->lines as $line) { |
94
|
|
|
$line->generate($this->height, $scaleToPx); |
95
|
|
|
|
96
|
|
|
$this->height += $line->height; |
97
|
|
|
$this->width = max($this->width, $line->width); |
98
|
|
|
|
99
|
|
|
// $line->image2->savePng($dirOut . count($breakpoints) . '.png'); // TODO remove debug |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $scaleToPx |
105
|
|
|
* |
106
|
|
|
* @return ImageContainer|null |
107
|
|
|
*/ |
108
|
|
|
public function generate($scaleToPx) { |
109
|
|
|
$this->renderLines($scaleToPx); |
110
|
|
|
|
111
|
|
|
// Recreating main sprite image with new width and height |
112
|
|
|
$this->imageReset(); |
113
|
|
|
|
114
|
|
|
// Generating final sprite |
115
|
|
|
$position = 0; |
116
|
|
|
foreach ($this->lines as $line) { |
117
|
|
|
$this->image->copyFrom($line->image, 0, $position); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$position += $line->height; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->image; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $scaleToPx |
127
|
|
|
* |
128
|
|
|
* @return string %3$s - $outName, %4$s - $relativeUrl |
129
|
|
|
*/ |
130
|
|
|
public function generateCss($scaleToPx) { |
131
|
|
|
$css = ''; |
132
|
|
|
foreach ($this->lines as $line) { |
133
|
|
|
$css .= $line->css; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return ".%3\$s{background-image: url('%4\$s%3\$s.png');display: inline-block;" . |
137
|
|
|
($scaleToPx > 0 ? "transform-origin: top left;" : "") . |
138
|
|
|
"}\n" . $css; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|