-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.php
74 lines (58 loc) · 1.81 KB
/
merge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
echo "listing files...\n";
error_reporting(E_ALL);
define('TILE_WIDTH', 256);
define('TILE_HEIGHT', 256);
define('EXPORT_DIRECTORY', './build/');
define('MAX_TILES_PER_MERGE', 50);
/// listing
$tiles = array();
$filesy = scandir('./tiles/16');
for ($i=0; $i < count($filesy); $i++) {
$filesx = scandir('./tiles/16/'.$filesy[$i]);
$tilesline = array();
for ($j=0; $j < count($filesx); $j++) {
if($filesx[$j] !== '.' && $filesx[$j] !== '..' && $filesy[$i] !== '.' && $filesy[$i] !== '..') {
array_push($tilesline,'./tiles/16/'.$filesy[$i].'/'.$filesx[$j]);
}
}
// do not push empty array
if(isset($tilesline[0])) {
array_push($tiles, $tilesline);
}
}
echo '- listed about '.(count($tiles) * count($tiles[0])).' files.'."\n";
/// merging
echo "merging files...\n";
$nbtilesy = count($tiles);
$nbtilesx = count($tiles[0]);
$startx = 0;
$pertiles = MAX_TILES_PER_MERGE;
while($startx < $nbtilesx) {
$starty = 0;
while($starty < $nbtilesy) {
$perx = min($pertiles, $nbtilesx - $startx);
$pery = min($pertiles, $nbtilesy - $starty);
generate($tiles, $startx, $starty, $perx, $pery);
$starty = $starty + $pery;
}
$startx = $startx + $perx;
}
function generate($tiles, $startx, $starty, $perx, $pery){
$saveTo = EXPORT_DIRECTORY.'result-'.$starty.'-'.$startx.'.png';
$image = imagecreate(TILE_WIDTH * $perx, TILE_HEIGHT * $perx);
for ($y=0; $y < $pery; $y++) {
for ($x=0; $x < $perx ; $x++) {
$filename = $tiles[($starty + $y)][($startx + $x)];
$tile = imagecreatefrompng($filename);
if($tile === false) {
var_dump('failed to create from '.$filename);
}
else {
imagecopy($image, $tile, $x * TILE_WIDTH, $y * TILE_HEIGHT, 0, 0, TILE_WIDTH, TILE_HEIGHT);
}
}
}
imagepng($image, $saveTo);
echo '- wrote '.$saveTo .' '.TILE_WIDTH * $perx.'*'.TILE_HEIGHT * $pery.'px.'."\n";
}