Both failure modes showed up in real use and neither was flagged anywhere. A single screenshot in the folder produced a "stitch" of one image with no warning at all, and the resulting ~900px map is upscaled on screen. The stitcher now says plainly that there is nothing to stitch, prints the resulting zoom levels, and warns below 4000px edge length; make_map_tiles.php warns on the same threshold, so it catches the case regardless of which tool is used. The documented --crop example was copied verbatim and silently removed 180px from the bottom of a real map. The example is gone: the plain command has no --crop, and the option is now described as optional with values that must be measured on one's own screenshots. The stitcher also reports what --crop removes on every run. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
226 lines
8.6 KiB
PHP
226 lines
8.6 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";
|
|
|
|
// Wie weit sich die Karte spaeter scharf heranzoomen laesst, haengt einzig an der
|
|
// Aufloesung dieser Vorlage - bei einem einzelnen Vollbild-Screenshot ist das zu wenig.
|
|
if (max($width, $height) < 4000) {
|
|
fwrite(STDERR, "\nHINWEIS: " . max($width, $height) . " Pixel Kantenlaenge sind wenig. Die Karte laesst\n"
|
|
. "sich damit nur bis Zoomstufe {$maxZoom} scharf anzeigen und wirkt naeher herangezoomt unscharf.\n"
|
|
. "Mehr Pixel bekommst du, indem du die Ingame-Karte maximal herangezoomt in Bahnen\n"
|
|
. "abfotografierst und die Screenshots mit tools/stitch_map_screenshots.py verbindest.\n\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;
|
|
}
|