<?php
	
	// large images require a lot of memory. If you're experiencing fatal error due to 
	// lack of memory, increase memory_limit flag
	
	ini_set("memory_limit","512M");	

	// input data (configuration)
	
	$imagePath = "velika.jpg";		// path to the large image
	$mapName   = "map";				// first part of map tiles name
	$mapDir	   = "map";				// directory to hold tiles (directory must be created manualy)
	$tilew	= 1000;					// width of tile
	$tileh	= 1000;					// height of tile
	
	// we have all we need, let's do the work
	
	// load large image
	
	$img = imagecreatefromjpeg($imagePath);
	
	// get width and height of image
	
	list($width, $height) = getimagesize($imagePath);
	
	$curx = 0;
	$cury = 0;
	
	// create tiles 
	
	while ($cury < $height) {
	
		// create new image and paste croped part to it
		
		$curTile = imagecreatetruecolor($tilew, $tileh);	
		imagecopyresampled($curTile, $img, 0, 0, $curx, $cury, $tilew, $tileh, $tilew, $tileh);

		// generate tile name
		
		$tileName = $mapName . '_' . $curx . '_' . $cury . '.jpg';
		
		// save tile and free used memory
		
		imagejpeg($curTile, $mapDir.'/'.$tileName);
		imagedestroy($curTile);
		
		// go to next position for croping
		
		$curx += $tilew;
		
		if ($curx > $width) {
			$curx = 0;
			$cury += $tileh;
		}
	}
		
	imagedestroy($img);
	
	echo "Finished.\n";
?>