<?php
 
#error_reporting( E_ALL );
 #ini_set("display_errors",1);

  
$path=addslashes(rawurldecode($_GET[path]));
  
$zoomlevel=addslashes($_GET[zoom]);
  
  
$height=addslashes($_GET[height]);
  
$width=addslashes($_GET[width]);
  
  if(
$zoomlevel==''){$zoomlevel=5;}
  
  
 function 
zoomsteps($pix)
{if (
$pix<=256) {return 0;} 
if (
$pix<=513) {return 1;}
if (
$pix<=1027) {return 2;}
if (
$pix<=2055) {return 3;}
if (
$pix<=4111) {return 4;}
if (
$pix<=8223) {return 5;}
if (
$pix<=16447) {return 6;} 
if (
$pix<=32895) {return 7;}
if (
$pix<=65791) {return 8;} else {exit;}


function 
pixinstep($pixmax,$zoomm,$zoomsteps)
 {  
$pixcounter=$pixmax;
 for (
$z $zoomsteps$z >= $zoomm+1$z--)
    {
$pixcounter=floor($pixcounter/2);
    }
    return 
$pixcounter;
 }

  
  
$xmlfile=file_get_contents($path."/ImageProperties.xml");
 
$path." Content: ".$xmlfile;

if (
$height=="")
{
$page array_pop explode 'WIDTH="' $xmlfile ) ) ;
$xabs array_shift explode '" ' $page ) ) ;

$page array_pop explode 'HEIGHT="' $xmlfile ) ) ;
$yabs array_shift explode '" ' $page ) ) ;

    if (
$xabs=="")    
        {echo 
'<HTML>
        <HEAD>
            
        <META HTTP-EQUIV="Pragma" CONTENT="no-cache"><META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <TITLE>Please copy height and width to the form </TITLE></HEAD>
<frameset rows="84,*" frameborder="no">\n    <frame src="./zoomify-form.php?zoom='
.$zoomlevel.'&path='.$path.'">\n<frame src="';
echo 
$path.'ImageProperties.xml"></frameset></HTML>';
        }

}

else {
$xabs=$width;
$yabs=$height;
}




if(
$xabs>$yabs) {$zoomsteps=zoomsteps($xabs);} else {$zoomsteps=zoomsteps($yabs);}

echo 
"<html><header><title>Zoomify $path x=$xabs y=$yabs maxzoom=$zoomsteps</title>";
echo 
'<style type="text/css"> body { margin:0; padding:0; } img {vertical-align:bottom;}</style> </header>';
echo 
"<body>";

#echo $xml->IMAGE_PROPERTIES[0]->;

echo '<table border="0" cellpadding="0" cellspacing="0" >';
 

$tilegroup=0;
$tilegroupcounter=0;


 for (
$zoom 0$zoom <= $zoomsteps$zoom++) {
 
$xlevelpix=ceil(pixinstep($xabs,$zoom,$zoomsteps)/256);
 
$ylevelpix=ceil(pixinstep($yabs,$zoom,$zoomsteps)/256);
 
#echo  $zoom.": ".pow(2,($zoomsteps-$zoom)).": ".$xlevelpix."-".$ylevelpix."<br>";
    
for ($y 0$y $ylevelpix$y++) {
            if(
$zoom==$zoomlevel) {echo "<tr>";}
        for (
$x 0$x $xlevelpix$x++) {
    
    if (
$tilegroupcounter>=256) {$tilegroup++;$tilegroupcounter=0;
        
#echo "GROUP:".$tilegroup." x=$x  y=$y";
    
}
    
$tilegroupcounter++;

    
if(
$zoom==$zoomlevel)
{  
    echo 
"<td>";    
  
$url="$path/TileGroup$tilegroup/$zoom-$x-$y.jpg";
    echo 
"<img alt=\"$zoom-$x-$y\" src=\"$url\"> \n";
        echo 
"<td>";
 }
                }
  if(
$zoom==$zoomlevel){echo "</tr>\n";}
    }
 
 }
 
?>
</table>
</body>
</html>