<?php
/*
****************************************************************************

unzoomify v0.1
(c) Shane Wright
www.shivars.org
me@shanewright.co.uk

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details: <http://www.gnu.org/licenses/gpl.txt>

****************************************************************************
*/


/***************************************************************************/
// handy bits

function debug($message) {
	echo "$message\n";
}

function death($reason) {
	echo "Error: $reason\n";
	exit(255);
}


function usage() {
	debug('unzoomify - a tool to stitch back together image tiles made by Zoomify');
	debug('');
	debug('Usage:');
	debug('  php unzoomify.php <path> <zoomlevel>');
	debug('Parameters:');
	debug('  path        path to the main directory for the image, e.g. ~/images/MyImage/');
	debug('  zoomlevel   the zoom level for the tiles you want to reassemble, e.g. 4');
}


/***************************************************************************/
// initialisation

$filename = 'output';
$baseurl = @$argv[1];
$zoomlevel = @$argv[2];

if (!(strlen($baseurl) > 0))
	death('you must pass the base file location as the first parameter');

if (!is_numeric($zoomlevel))
	death('you must specify the zoom level required as the second parameter');

if (strlen(basename($baseurl)) > 0) {
	$filename = basename($baseurl);
	debug("Output filename: $filename.jpg");
};

/***************************************************************************/
// initialisation

$tiles = Array();

$x = 0;
$y = 0;
$maxx = 99;
$maxy = 99;


function buildurl($baseurl, $zoom, $x, $y) {
	return "$baseurl/TileGroup0/$zoom-$x-$y.jpg";
}


while ($y < $maxy) {
	
	$x = 0;
	while ($x < $maxx) {
		
		$url = buildurl($baseurl, $zoomlevel, $x, $y);
		
		$im = @imagecreatefromjpeg($url);
		
		if ($im) {
			debug("Opened tile for ($x,$y)");
			
			$tiles[$x][$y] = $im;
			
		} else {
			// not a valid part of it
			debug("($x,$y) not a valid tile\n");
			if ($x > 0) {
				debug("Determined image is $x tiles wide\n");
				$maxx = $x;
			} else {
				debug("Determined image is $y tiles tall\n");
				$maxy = $y;
				$x = $maxx;
			};
		};
		
		$x++;
	}
	
	$y++;
}


debug("");
debug("Got image $maxx by $maxy tiles in size");

/***************************************************************************/
// create overall image


$width  = 0;
$height = 0;

// calc width
for ($x=0; $x<$maxx; $x++)
	$width += imagesx($tiles[$x][0]);
// calc height
for ($y=0; $y<$maxy; $y++)
	$height += imagesy($tiles[0][$y]);

debug("Full image size is $width x $height");

$im = imagecreatetruecolor($width, $height);

$py = 0;
for ($y=0; $y<$maxy; $y++) {
	$px = 0;
	$size_y = 0;
	for ($x=0; $x<$maxx; $x++) {
		if ($tiles[$x][$y]) {
			$size_x = imagesx($tiles[$x][$y]);
			$size_y = imagesy($tiles[$x][$y]);
			
			imagecopy($im, $tiles[$x][$y], $px, $py, 0, 0, $size_x, $size_y);
			
			$px += $size_x;
		};
	};
	
	$py += $size_y;
}

imagejpeg($im, $filename . '.jpg', 80);

debug("Done!");

?>
