Adds tools/stitch_map_screenshots.py, which assembles overlapping in-game map screenshots into one large image. How far the map can be zoomed in sharply depends only on the source resolution - the tiler stops at 1:1 - so the way to a sharp map is capturing a grid of screenshots at maximum in-game zoom rather than one full-map screenshot. Offsets are found by FFT phase correlation and verified by the normalised cross correlation of the actual overlap, so filename order does not matter. Because phase correlation is cyclic, both readings of every peak are tried per axis - shifts beyond half the image width are the normal case at 35% overlap. Overlaps are composited by nearest image centre, which keeps seams away from the frame edges without the blur of feathering. Verified against a reference image: 12 shuffled screenshots with UI bands reassemble pixel-identically. Sharpness on the website side: the tile layer now uses detectRetina, so high-density displays get the next tile level drawn at half size instead of an upscaled, blurry top level. Default JPEG quality raised from 82 to 88, which suits map labels better. The GD check in make_map_tiles.php now reports the loaded php.ini, the extension_dir and the exact lines to add - a stock Windows/scoop PHP loads no php.ini at all, so the previous one-line error left no way forward. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
217 lines
7.9 KiB
PHP
217 lines
7.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Zerlegt ein grosses Kartenbild in eine Kachel-Pyramide fuer die Taktikkarte (map.php).
|
|
*
|
|
* Aufruf (lokal, nicht auf dem Webspace noetig):
|
|
*
|
|
* php tools/make_map_tiles.php <kartenschluessel> <bilddatei> [--name="Anzeigename"]
|
|
* [--tile-size=256] [--quality=88] [--out=<zielordner>]
|
|
*
|
|
* Beispiel:
|
|
*
|
|
* php -d memory_limit=2G tools/make_map_tiles.php verteron "C:\Bilder\verteron.png" --name="Verteron"
|
|
*
|
|
* Ergebnis landet in assets/uploads/maps/<kartenschluessel>/ und besteht aus einem Ordner
|
|
* je Zoomstufe plus einer tiles.json, ueber die sich die Karte selbst beschreibt. Diesen
|
|
* Ordner danach einfach per FTP hochladen - map.php findet ihn von allein.
|
|
*
|
|
* Die oberste Zoomstufe zeigt das Bild in Originalaufloesung, jede Stufe darunter ist halb
|
|
* so gross, bis die ganze Karte in eine einzige Kachel passt. Randkacheln werden auf die
|
|
* volle Kachelgroesse aufgefuellt (mit der Hintergrundfarbe der Website), damit Leaflet sie
|
|
* nicht verzerrt.
|
|
*/
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
fwrite(STDERR, "Dieses Script laeuft nur auf der Kommandozeile.\n");
|
|
exit(1);
|
|
}
|
|
|
|
if (!extension_loaded('gd')) {
|
|
// Haeufigster Fall auf einer frischen Windows-/scoop-Installation: PHP findet gar keine
|
|
// php.ini und laedt deshalb ueberhaupt keine Erweiterung. Deshalb hier nicht nur melden,
|
|
// was fehlt, sondern auch gleich wo es einzutragen waere.
|
|
$loadedIni = php_ini_loaded_file();
|
|
$expectedIni = $loadedIni !== false ? $loadedIni : dirname(PHP_BINARY) . DIRECTORY_SEPARATOR . 'php.ini';
|
|
$extensionDir = (string) ini_get('extension_dir');
|
|
$extensionHint = $extensionDir === ''
|
|
? '(leer)'
|
|
: $extensionDir . (is_dir($extensionDir) ? '' : ' <- existiert nicht');
|
|
|
|
$quoted = array_map(
|
|
static fn (string $arg): string => str_contains($arg, ' ') ? '"' . $arg . '"' : $arg,
|
|
$argv
|
|
);
|
|
|
|
fwrite(STDERR, "Die PHP-Erweiterung GD wird benoetigt, ist aber nicht geladen.\n\n");
|
|
fwrite(STDERR, ' PHP: ' . PHP_BINARY . ' (' . PHP_VERSION . ")\n");
|
|
fwrite(STDERR, ' php.ini: ' . ($loadedIni !== false ? $loadedIni : 'KEINE geladen') . "\n");
|
|
fwrite(STDERR, ' extension_dir: ' . $extensionHint . "\n\n");
|
|
fwrite(STDERR, "So beheben - in\n\n " . $expectedIni . "\n\ndiese beiden Zeilen eintragen"
|
|
. ($loadedIni !== false ? '' : ' (Datei dafuer neu anlegen)') . ":\n\n");
|
|
fwrite(STDERR, " extension_dir = \"ext\"\n extension=gd\n\n");
|
|
fwrite(STDERR, "Danach mit 'php -m' pruefen, ob 'gd' in der Liste steht.\n\n");
|
|
fwrite(STDERR, "Einmalig ohne Konfigurationsdatei geht es auch direkt:\n\n php -d extension_dir=\""
|
|
. dirname(PHP_BINARY) . DIRECTORY_SEPARATOR . "ext\" -d extension=gd " . implode(' ', $quoted) . "\n");
|
|
exit(1);
|
|
}
|
|
|
|
$args = array_slice($argv, 1);
|
|
$positional = [];
|
|
$options = ['name' => null, 'tile-size' => 256, 'quality' => 88, 'out' => null];
|
|
|
|
foreach ($args as $arg) {
|
|
if (str_starts_with($arg, '--')) {
|
|
[$key, $value] = array_pad(explode('=', substr($arg, 2), 2), 2, '');
|
|
if (!array_key_exists($key, $options)) {
|
|
fwrite(STDERR, "Unbekannte Option: --$key\n");
|
|
exit(1);
|
|
}
|
|
$options[$key] = $value;
|
|
} else {
|
|
$positional[] = $arg;
|
|
}
|
|
}
|
|
|
|
if (count($positional) < 2) {
|
|
fwrite(STDERR, "Aufruf: php tools/make_map_tiles.php <kartenschluessel> <bilddatei> [--name=\"Anzeigename\"]\n");
|
|
exit(1);
|
|
}
|
|
|
|
[$mapKey, $sourcePath] = $positional;
|
|
|
|
if (preg_match('/^[a-z0-9_-]+$/', $mapKey) !== 1) {
|
|
fwrite(STDERR, "Der Kartenschluessel darf nur Kleinbuchstaben, Ziffern, - und _ enthalten (z.B. 'verteron').\n");
|
|
exit(1);
|
|
}
|
|
if (!is_file($sourcePath)) {
|
|
fwrite(STDERR, "Bilddatei nicht gefunden: $sourcePath\n");
|
|
exit(1);
|
|
}
|
|
|
|
$tileSize = max(64, (int) $options['tile-size']);
|
|
$quality = min(100, max(40, (int) $options['quality']));
|
|
$displayName = $options['name'] !== null && $options['name'] !== '' ? (string) $options['name'] : ucfirst($mapKey);
|
|
$outputRoot = $options['out'] !== null && $options['out'] !== ''
|
|
? rtrim((string) $options['out'], '/\\')
|
|
: __DIR__ . '/../assets/uploads/maps/' . $mapKey;
|
|
|
|
echo "Lade Bild …\n";
|
|
$source = load_image($sourcePath);
|
|
$width = imagesx($source);
|
|
$height = imagesy($source);
|
|
echo "Quellbild: {$width} x {$height} Pixel\n";
|
|
|
|
// Hoechste Zoomstufe = Originalgroesse. Kleinste Stufe = das ganze Bild passt in eine Kachel.
|
|
$maxZoom = (int) max(0, ceil(log(max($width, $height) / $tileSize, 2)));
|
|
$minZoom = 0;
|
|
|
|
echo "Zoomstufen: {$minZoom} bis {$maxZoom} (Kachelgroesse {$tileSize}px)\n";
|
|
|
|
if (!is_dir($outputRoot) && !mkdir($outputRoot, 0775, true) && !is_dir($outputRoot)) {
|
|
fwrite(STDERR, "Zielordner konnte nicht angelegt werden: $outputRoot\n");
|
|
exit(1);
|
|
}
|
|
|
|
$totalTiles = 0;
|
|
|
|
for ($zoom = $maxZoom; $zoom >= $minZoom; $zoom--) {
|
|
$scale = 2 ** ($zoom - $maxZoom);
|
|
$levelWidth = max(1, (int) round($width * $scale));
|
|
$levelHeight = max(1, (int) round($height * $scale));
|
|
|
|
echo "Zoom {$zoom}: {$levelWidth} x {$levelHeight} … ";
|
|
|
|
$level = $zoom === $maxZoom ? $source : resize_image($source, $levelWidth, $levelHeight);
|
|
|
|
$cols = (int) ceil($levelWidth / $tileSize);
|
|
$rows = (int) ceil($levelHeight / $tileSize);
|
|
|
|
$zoomDir = $outputRoot . '/' . $zoom;
|
|
if (!is_dir($zoomDir) && !mkdir($zoomDir, 0775, true) && !is_dir($zoomDir)) {
|
|
fwrite(STDERR, "\nOrdner konnte nicht angelegt werden: $zoomDir\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Kein imagedestroy() noetig: GD-Bilder raeumt PHP seit 8.0 selbst auf, und seit 8.5
|
|
// ist die Funktion als deprecated markiert.
|
|
for ($x = 0; $x < $cols; $x++) {
|
|
for ($y = 0; $y < $rows; $y++) {
|
|
$tile = imagecreatetruecolor($tileSize, $tileSize);
|
|
// Randkacheln auf volle Groesse auffuellen - in der Hintergrundfarbe der
|
|
// Website, damit der Uebergang am Kartenrand nicht auffaellt.
|
|
imagefill($tile, 0, 0, imagecolorallocate($tile, 0x14, 0x16, 0x1a));
|
|
|
|
$srcX = $x * $tileSize;
|
|
$srcY = $y * $tileSize;
|
|
$copyWidth = min($tileSize, $levelWidth - $srcX);
|
|
$copyHeight = min($tileSize, $levelHeight - $srcY);
|
|
|
|
imagecopy($tile, $level, 0, 0, $srcX, $srcY, $copyWidth, $copyHeight);
|
|
imagejpeg($tile, $zoomDir . '/' . $x . '_' . $y . '.jpg', $quality);
|
|
$totalTiles++;
|
|
}
|
|
}
|
|
|
|
echo $cols * $rows . " Kacheln\n";
|
|
}
|
|
|
|
$manifest = [
|
|
'name' => $displayName,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
'tile_size' => $tileSize,
|
|
'min_zoom' => $minZoom,
|
|
'max_zoom' => $maxZoom,
|
|
'extension' => 'jpg',
|
|
'generated_at' => date('c'),
|
|
];
|
|
|
|
file_put_contents(
|
|
$outputRoot . '/tiles.json',
|
|
json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
|
);
|
|
|
|
echo "\nFertig: {$totalTiles} Kacheln in {$outputRoot}\n";
|
|
echo "Diesen Ordner nach assets/uploads/maps/{$mapKey}/ auf den Webspace hochladen.\n";
|
|
|
|
/**
|
|
* Laedt JPEG, PNG oder WebP als GD-Bild.
|
|
*
|
|
* @return GdImage
|
|
*/
|
|
function load_image(string $path)
|
|
{
|
|
$info = @getimagesize($path);
|
|
if ($info === false) {
|
|
fwrite(STDERR, "Datei ist kein unterstuetztes Bild: $path\n");
|
|
exit(1);
|
|
}
|
|
|
|
$image = match ($info[2]) {
|
|
IMAGETYPE_JPEG => @imagecreatefromjpeg($path),
|
|
IMAGETYPE_PNG => @imagecreatefrompng($path),
|
|
IMAGETYPE_WEBP => @imagecreatefromwebp($path),
|
|
default => false,
|
|
};
|
|
|
|
if ($image === false) {
|
|
fwrite(STDERR, "Bild konnte nicht geladen werden (JPEG, PNG und WebP werden unterstuetzt).\n");
|
|
fwrite(STDERR, "Bei sehr grossen Bildern hilft ein hoeheres Speicherlimit: php -d memory_limit=2G ...\n");
|
|
exit(1);
|
|
}
|
|
|
|
return $image;
|
|
}
|
|
|
|
/**
|
|
* @param GdImage $source
|
|
* @return GdImage
|
|
*/
|
|
function resize_image($source, int $width, int $height)
|
|
{
|
|
$resized = imagecreatetruecolor($width, $height);
|
|
imagecopyresampled($resized, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
|
|
return $resized;
|
|
}
|