Skip to content

Instantly share code, notes, and snippets.

@Fil
Last active August 29, 2015 14:10
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Fil/8d5490a1b205a77809e0 to your computer and use it in GitHub Desktop.
make a large png from map tiles
<?php
# stitch tile images together to form a large image
# see also https://github.com/ericfischer/tile-stitch
function usage($err= null) {
echo "php recolle.php 'http://TILES_SERVER/%z/%x/%y.png' z x1 y1 [+nx] [+ny]\n";
if ($err)
echo "$err\n";
exit;
}
list(,$pattern, $z, $x1, $y1, $nx, $ny) = $argv;
$z = intval($z);
$x1 = intval($x1);
$y1 = intval($y1);
// by defaut 16x16
if (is_null($nx)) $nx = 16;
if (is_null($ny)) $ny = 16;
$nx = intval($nx);
$ny = intval($ny);
if ($z<0 OR $z > 17) usage ('0 <= z <= 17');
if ($nx * $ny > 1000000) usage ('1 million de tuiles max');
$map = array();
for ($i = 0; $i<=$nx; $i++) {
$map[$i] = array();
for ($j = 0; $j<=$ny; $j++) {
$map[$i][$j] = get_tile($pattern, $z, $x1+$i, $y1+$j);
var_dump($map[$i][$j]);
}
}
if ($a = glob('tmp/*.png')) array_map('unlink', $a);
$c = '';
for ($i = 0; $i<=$nx; $i++) {
$x = $x1+$i;
$b = join(' ', array_map('escapeshellarg', $map[$i]));
$b = "montage -background none -alpha on -geometry 256x256:+0+0 ".$b." -tile 1x tmp/$x.png";
echo "montage bande $x...\n$b\n";
`$b`;
$c .= " tmp/$x.png";
}
echo "montage des bandes...\n";
$b = "montage -background none -alpha on -mode Concatenate -tile x1$c tmp/full.png";
`$b`;
`open tmp/full.png`;
function get_tile($pattern, $z, $x, $y) {
$url = str_replace(array('%z', '%x', '%y'), array($z, $x, $y), $pattern);
$tile = "tmp/$z/$x/$y";
if ($a = glob($tile.'.{png,jpg,tmp}', GLOB_BRACE))
return preg_match(',(png|jpg)$,', $a[0]) ? $a[0] : 'vide.png';
@mkdir (dirname($tile), 0755, true);
$_url = escapeshellarg($url);
$_tile = escapeshellarg($tile.'.tmp');
`wget $_url -O $_tile`;
$check = `identify -quiet $_tile`;
if (preg_match(', (PNG|JPEG),', $check, $r)) {
rename ($tile.'.tmp', $dest = $tile.'.'.strtolower($r[1]));
return $dest;
}
return 'vide.png'; # null: pour imagemagick/ montage
# http://www.imagemagick.org/Usage/montage/#zero_geometry
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment