<?
header('Content-type: application/xml; charset="utf-8"',true);
   $picdir = "./images";
   $picdir = "./layouts/".$_REQUEST['m'];

   $dirlist = array();

   if ($handle = @opendir( $picdir)) {
      $i=0;
      while (false !== ($file = readdir($handle))) {
         if ($file == "." || $file == "..")
         continue;

         if ( substr(strtolower($file),-4) == ".jpg"){
           // 'bannerimg' is magic, so skip it
           if ( strpos( strtolower($file), "bannerimg") !== false )
               continue;
           $newfile = makeThumb( $picdir."/".$file, $picdir."/th_");
            $base = basename($file,".jpg");
            $text = file_exists( $picdir."/".$base.".txt" ) ? file_get_contents($picdir."/".$base.".txt") : "-";
            $dirlist[ $i++ ] = array( "base"=>$base, "text"=>$text);
         }
      }
      closedir($handle);
   }

   usort($dirlist, "picsort" );

   printf("<picinfo>\n");
   for ( $i = 0 ; $i < count($dirlist) ; $i++ ) {
      printf("<pic>\n");
         printf("<dir>%s</dir>\n", $picdir);
         printf("<base>%s</base>\n", $dirlist[$i]["base"]);
         printf("<text>%s</text>\n", $dirlist[$i]["text"]);
      printf("</pic>\n");
   }

   // end the document and output
   printf("</picinfo>\n");


function picsort($a, $b) {
   return( strcasecmp($a["base"],$b["base"]) );
}

function makeThumb( $orig, $destpathpart ) {

    // =================================
    // == CONFIGURATION 
    // ==
    // ==
    // specify target width, height or both
    $targetW = 128;
    $targetH = 0;    // calculate it, same aspect ratio

    // ==
    // ==
    // into which destination directory go the resizes?
    //$destpathpart = "/tmp/";

    // =================================
    // Parameter Manipulation
    // ==
    // ==
    // What imagetype should the new file be?
    $newtype = ( strpos($orig,".") !== false ) ? substr($orig, strrpos($orig,".")+1) : false;
    $newtype = "jpeg";
    $newtype = "jpg";
    $newtype = "png";
    $newtype = "gif";

    // ==
    // ==
    // What is the basename of the new file?
    $newbase = basename($orig);
    $newbase = substr($newbase, 0, strrpos($newbase,"."));

    // Fin.
    // =================================



    $dest = $destpathpart.$newbase.".".$newtype;

    if ( !file_exists($dest) ) {
       $info = getimagesize($orig);
       if ( !($targetW || $targetH) )
          die("targetW or targetH required");
       if ( !($targetW && $targetH) ) {
          // have only one
          if ( $targetW )
             $targetH = $info[1] * ($targetW/$info[0]);
          else
             $targetW = $info[0] * ($targetH/$info[1]);
       }

       $rc = resizeit( $orig, $dest, $targetW, $targetH );
       clearstatcache();
       return( ($rc) ? $dest : false );
    }
    return( $dest );
}


function resizeit( $orig, $dest, $neww, $newh)
{
   $otype = ( strpos($orig,".") !== false ) ? substr($orig, strrpos($orig,".")+1) : false;
   $ntype = ( strpos($dest,".") !== false ) ? substr($dest, strrpos($dest,".")+1) : false;

   list($width, $height) = getimagesize($orig);

   $image_p = imagecreatetruecolor($neww, $newh);

   switch ( $otype ) {
   case "gif":
      $img = imagecreatefromgif($orig);
      break;
   case "png":
      $img = imagecreatefrompng($orig);
      break;
   case "jpg":
   case "jpeg":
      $img = imagecreatefromjpeg($orig);
      break;
   default:
      return(false);
   }

   $rc = imagecopyresampled($image_p, $img, 0, 0, 0, 0, $neww, $newh, $width, $height);

   switch( $ntype ) {
   case "gif":
      $rc = imagegif($image_p, $dest);
      break;
   case "png":
      if ( !function_exists('imagepng') )
         die("can't create png");
      $rc = imagepng($image_p, $dest, 9);
      break;
   case "jpg":
   case "jpeg":
      $rc = imagejpeg($image_p, $dest, 100);
      break;
   default:
      return( false );
   }

   chmod( $dest, 0666);
   return( true );
}
?>
