--- qdig-20060105cnj.php	Sun Jan 15 16:06:55 2006
+++ qdig-20070430cnj.php	Mon Apr 30 20:23:08 2007
@@ -16,7 +16,7 @@
 | for customizing your galleries.  The script runs stand-alone, or a
 | gallery may be included within another page.  Enjoy!
 +----------------------------------------------------------------------+
-| Portions Copyright 2005 Matthew Wronka <qdig@nonce.matt.wronka.org>
+| Portions Copyright 2005, 2006 Matthew Wronka <qdig@nonce.matt.wronka.org>
 | *   Any support questions with this script should be sent to Matt!   *
 | * Please do not ask Hagan or use the forums for the original script. *
 | Original script by Hagan Fox <http://qdig.sourceforge.net/>
@@ -37,9 +37,9 @@
 | Version 2 along with this program; if not, visit GNU's Home Page
 | http://www.gnu.org/
 +----------------------------------------------------------------------+
-CVS: $Id: qdig.php,v 1.29 2006/01/06 01:11:36 matt Exp $#
+CVS: $Id: qdig.php,v 1.35 2007/05/01 00:21:26 matt Exp $#
 */#
-$qdig_version = '20060105cnj [based on 20050509]';
+$qdig_version = '20070430cnj [based on 20050509]';
 $mtime = microtime();
 $mtime = explode(' ', $mtime);
 $start_time = $mtime[1] + $mtime[0];
@@ -616,6 +618,7 @@
 /**
 * Path Settings  (Override defaults.)
 */
+$qdig_url_alias='gallery/';	// if set, will use this.'/$Qwd/$Qif'
 $qdig_url = '/pictures/'; // Self-referring URL path.  Examples: '/photos/' or
                 // '/photos/qdig.php' or '/~someuser/qdig/index.php'
 // The next two are the same location; as a URL and as a filesystem path.
@@ -630,6 +633,7 @@
 $excl_dirs[] = 'Private';        // | Ignore a directory with its name
 $excl_dirs[] = 'qdig-converted'; // | included here.  Do not  end these
 $excl_dirs[] = 'CVS';               // | name(s) with '/'.
+$excl_dirs[] = 'qdig-files';               // 
 $excl_dirs[] = '';               // 
 $excl_imgs[] = 'qdig-bg.jpg'; // | Ignore any image with its name
 $excl_imgs[] = 'favicon.png'; // | included here.  Add as many of
@@ -640,7 +644,7 @@
                       // Examples: 'incl=qdig.php&amp;' 'a=foo&amp;b=bar&amp;'
 $keep_params = FALSE; // Retain extra GET parameters in Qdig URLs.  Allows
                       // Qdig to "share" the query string when embedded. // TODO: Set limits (#/size)? Rename?
-$anchor = '';         // Include an intra-page anchor in URLs.  For embedded
+$anchor = '#qdig-dirnav';         // Include an intra-page anchor in URLs.  For embedded
                       // galleries, use '#qdig' to jump down to the gallery.
 
 /**
@@ -1145,7 +1149,8 @@
 		{
 			$img_size = GetImageSize($orig_img);
 			$height = $img_size[1];
-			$th_maxdim = $height;
+			$width  = $img_size[0];
+			$th_maxdim = ($height > $width/2)?$height:$width/2;
 			$cnvt_percent = round(($cnvrt_thmb['size'] / $th_maxdim) * 100, 2);
 			$cnvrtd_img.='[0]'; // TODO: Do we always want the 0th scene?
 
@@ -1538,7 +1543,8 @@
 function dirnavPath($dir_nav, $thm_dir_view = FALSE)
 {
 	global $get_vars, $imgs, $subdirs, $qdig_url, $extra_param, $anchor,
-		$reqd_image, $img_link, $dir_nav, $rootdir, $thmb_row, $cnvrt_thmb;
+		$reqd_image, $img_link, $dir_nav, $rootdir, $thmb_row, 
+		$cnvrt_thmb, $qdig_url_alias;
 	$i = 0;
 	$num_imgs = count($imgs);
 	$split_path = explode('/', urlPath($reqd_image['pwd']));
@@ -1646,7 +1652,9 @@
 */
 function getDirectoryImage($root, $bThmb=false)
 {
-	global $url_base_path, $pwd, $cnvrt_files_root, $cnvrtd_dir, $cnvrt_thmb;
+	global $url_base_path, $pwd, $cnvrtd_files_root, $cnvrtd_dir, $cnvrt_thmb;
+	global $qdig_files, $convrtd_subdir;
+
 	$dirs=array('');
 	while(sizeof($dirs)) {
 		shuffle($dirs);
@@ -1654,15 +1662,59 @@
 			$dir_imgs = getImageFilenames($root.'/'.$dir);
 			$rval = $dir_imgs[rand(0,sizeof($dir_imgs)-1)];
 			if($rval!='') {
-				if(!$bThmb) return $rval;
-				$dcnvrt_path   = cleanPath($cnvrtd_files_root.$root.'/'.$dir.'/'.$cnvrtd_dir);
-				$thmb_file = $url_base_path.'/'.$dcnvrt_path.'/'.'/'.$cnvrt_thmb['prefix'].$rval.'.jpeg';
-				return $thmb_file;
+				if(!$bThmb) {
+					$img_size = GetImageSize($rval);
+				} else {
+	/* XXX TODO: major entire code-base cleanup of cnvrtd_path stuff */
+		/* Make stuff work, cleanly, with qdig-files.  This works, not clean. */
+					if(!isset($qdig_files) || !$qdig_files) {
+						$dcnvrt_path   = cleanPath($cnvrtd_files_root.$root.'/'.$dir.'/'.$cnvrtd_dir);
+					} else {
+						$dcnvrt_path   = cleanPath($cnvrtd_files_root.$qdig_files.'/'.$convrtd_subdir.'/'.$root.'/'.$dir.'/'.$cnvrtd_dir);
+					}
+					$thmb_file = $fs_base_path.$dcnvrt_path.'/'.'/'.$cnvrt_thmb['prefix'].$rval.'.jpeg';
+					$thmb_path = $url_base_path.'/'.$dcnvrt_path.'/'.'/'.$cnvrt_thmb['prefix'].$rval.'.jpeg';
+					if(!file_exists($thmb_file)) {
+						continue;
+					}
+					$rval = $thmb_path;
+					$img_size = GetImageSize($thmb_file);
+				}
+				$width = $img_size[0];
+				$height = $img_size[1];
+				return array($rval, $width, $height);
 			}
 		}
 		$root=$root.'/'.$dirs[0];
-		$dirs = array_keys(getDirNames($root));
+		$dirNames = getDirNames($root);
+		if(is_array($dirNames)) {
+			$dirs = array_keys($dirNames);
+		} else {
+			$dirs = array();
+		}
+	}
+}
+
+/**
+* Get image count for directory and subs
+*/
+function getDirectoryImageCount($root)
+{
+	global $url_base_path, $pwd, $cnvrtd_files_root, $cnvrtd_dir, $cnvrt_thmb;
+	global $qdig_files, $convrtd_subdir;
+
+	$rval = 0;
+	$dirNames = getDirNames($root);
+	if(is_array($dirNames)) {
+		$dirs = array_keys($dirNames);
+	} else {
+		$dirs = array();
 	}
+	foreach($dirs as $dir) {
+		$rval += getDirectoryImageCount($root . '/' . $dir);
+	}
+	$rval += sizeof(getImageFilenames($root));
+	return $rval;
 }
 
 /**
@@ -1671,7 +1723,7 @@
 function subdirLinks($dir_nav)
 {
 	global $imgs, $qdig_url, $extra_param, $anchor, $reqd_image, $subdirs,
-		$thumbs, $rootdir, $caption_path;
+		$thumbs, $rootdir, $caption_path, $qdig_url_alias;
 	if (isset($imgs)) {
 		if ($dir_nav['icon'] == 1) {
 			$icon = '<img src="'.$qdig_url.'?image=cam-icon" alt="camera icon" />&nbsp;';
@@ -1708,7 +1760,8 @@
 			$dirurl = rawurlencode($dir);
 			$dirtxt = str_replace(' ', '&nbsp;', $dir);  // replace spaces
 			$dirtxt = str_replace("'", '&#39;', $dir);   // replace apostrophes
-			$dircapt= file_exists("$caption_path/$dir.txt")?file_get_contents("$caption_path/$dir.txt"):"";
+			$dircapt= file_exists("$caption_path/$dir.txt")?file_get_contents("$caption_path/$dir.txt"):""; 
+			$dircapt.= '(' . getDirectoryImageCount("{$reqd_image['pwd']}/{$dir}") . ')';
 			if ($age_idx < ($dir_nav['dir_is_new'] * 1000)) {
 				$newdir = $dir_nav['new_flag'];
 			} else {
@@ -1717,12 +1770,20 @@
 			$rimg='';
 			if ($dir_nav['icon'] == 2 && !isset($imgs)) {
 				$rand_image = getDirectoryImage("{$reqd_image['pwd']}/$dir", True);
-				$rimg = '<img src="'.$rand_image.'" alt="Gallery Image" style="vertical-align:middle" />&nbsp;';
+				$rimg = '<img src="'.$rand_image[0].'" width="'
+					.$rand_image[1].'" height="'
+					.$rand_image[2]
+					.'" alt="Gallery Image" style="vertical-align:middle" />&nbsp;';
 				if($rand_image=='') $rimg='';
 			}
+			if(! isset($qdig_url_alias) || $qdig_url_alias=='') {
+				$href = "$qdig_url?{$extra_param}Qwd={$reqd_image['pwd_url']}/$dirurl{$qif}&amp;Qiv={$reqd_image['view']}&amp;Qis={$reqd_image['size']}$anchor";
+			} else {
+				$href = cleanPath("{$qdig_url}/{$qdig_url_alias}/{$reqd_image['pwd_url']}/$dirurl{$qif}/$anchor");
+			}
 			$str .= <<<EOT
     <span style='white-space:nowrap;'>$tag1$bkt1
-     <a  href="$qdig_url?{$extra_param}Qwd={$reqd_image['pwd_url']}/$dirurl{$qif}&amp;Qiv={$reqd_image['view']}&amp;Qis={$reqd_image['size']}$anchor"
+     <a  href="$href"
       class="qdig-subdirlink" title="{$dir_nav['go_to_txt']} $dir">$icon$rimg$dirtxt $dircapt $newdir</a>
     $bkt2</span>\n
 EOT;
@@ -1849,7 +1910,7 @@
 	global $get_vars, $pwd, $imgs, $qdig_url, $extra_param, $anchor,
 		$reqd_image, $cnvrt_path, $cnvrt_thmb_rv, $nav_lnk, $thumbs_msg,
 		$thumbs, $file_exists_disa, $cnvrt_mesgs, $url_base_path,
-		$diag_messages, $safe_mode, $header;
+		$diag_messages, $safe_mode, $header, $qdig_url_alias;
 	$cnvrt_thmb=$cnvrt_thmb_rv;
 	// set a default maxwidth (if necessary)
 	if (empty($thmb_row['maxwidth'])) { $thmb_row['maxwidth'] = 500; }
@@ -1920,9 +1981,19 @@
 			if ($caption['above'] == TRUE) {
 				$str .= captionBlock($thm['image'], $img_size[0]);
 			}
+			$img_size = GetImageSize($thm['thumb']);
+			$width    = $img_size[0];
+			$height   = $img_size[1];
+
+			if( (! isset($qdig_url_alias) || $qdig_url_alias=='') && $reqd_image['size'] != '') {
+				$href = "$qdig_url?{$extra_param}Qwd={$reqd_image['pwd_url']}&amp;Qif={$thm['imgurl']}&amp;Qiv={$reqd_image['view']}&amp;Qis={$reqd_image['size']}$anchor";
+			} else {
+				$href = cleanPath("{$qdig_url}/{$qdig_url_alias}/{$reqd_image['pwd_url']}/$dirurl{$qif}/{$thm['imgurl']}$anchor");
+			}
 			$str .= <<<EOT
-  <a href="$qdig_url?{$extra_param}Qwd={$reqd_image['pwd_url']}&amp;Qif={$thm['imgurl']}&amp;Qiv={$reqd_image['view']}&amp;Qis={$reqd_image['size']}$anchor"
+  <a href="{$href}"
    title="{$nav_lnk['Image']} {$thm['num']} - {$thm['image']}"><img class="$thumb_class" $thumb_id src="{$thm['cnvurl']}"
+   width="{$width}" height="{$height}"
    alt="{$thm['image']}" {$thm['img_size'][3]} /></a>\n
 EOT;
 			if ($caption['above'] != TRUE) {
@@ -2314,9 +2385,13 @@
 	}
 	$img_url = $size_str_url.rawurlencode($img_file.$suffix_string);
 	if (@$get_vars['Qtmp'] == 'popup') {
+		$img_size = GetImageSize($thm['thumb']);
+		$width    = $img_size[0];
+		$height   = $img_size[1];
 		$str .= <<<EOT
   <a href="javascript:window.close();" title='{$lang['Close Window']}'
   ><img class="qdig-image" src="$url_base_path$img_url"
+     width="{$width}" height="{$height}"
      alt="$img_file" {$img_size[3]} /></a>
 EOT;
 	} else if ($num_imgs > 0) { // TODO: Clean this up.
@@ -2491,7 +2566,7 @@
 } // End exifCameraBlock()
 
 /**
-* Display all(?) EXtended InFormation (in TIFFs and JPEGs)
+* Display all(?) EXchangeable image file information (in TIFFs and JPEGs)
 *
 * The block is as wide as the image or else $exif_verbose['min_width'] pixels.
 */
@@ -3906,7 +3981,7 @@
 */
 if ($thumbs['onfly'] == FALSE &&
    !(isset($GLOBALS['no_qdig_body']) &&  $GLOBALS['no_qdig_body'])) {
-	$thumbs_msg = createThumbs($cnvrt_thmb, fale);
+	$thumbs_msg = createThumbs($cnvrt_thmb, false);
 	$thumbs_msg = createThumbs($cnvrt_thmb_rv, true);
 }
 
@@ -4100,8 +4175,10 @@
 /**
 * Directory Navigation
 */
-$dirnav_out = dirNav($dir_nav);
-echo $dirnav_out['str'];
+if($dir_nav['above']) {
+	$dirnav_out = dirNav($dir_nav);
+	echo $dirnav_out['str'];
+}
 
 /**
 * Control Bar
