无论出于何种原因,我在网站上拥有的每个图库中的前两个图像均未正确显示。我查看了页面源代码,发现了这一点:
<td><a href="javascript:imgSubmit('image.php?gallery=053&imgnum=1');"><img src="/galleries/053/thumbs/." class="thumb_img" border="0" /></a></td>
<td><a href="javascript:imgSubmit('image.php?gallery=053&imgnum=2');"><img src="/galleries/053/thumbs/.." class="thumb_img" border="0" /></a></td>
<td><a href="javascript:imgSubmit('image.php?gallery=053&imgnum=3');"><img src="/galleries/053/thumbs/053img009.jpg" class="thumb_img" border="0" /></a></td>
<td><a href="javascript:imgSubmit('image.php?gallery=053&imgnum=4');"><img src="/galleries/053/thumbs/053img008.jpg" class="thumb_img" border="0" /></a></td>
<td><a href="javascript:imgSubmit('image.php?gallery=053&imgnum=5');"><img src="/galleries/053/thumbs/053img011.jpg" class="thumb_img" border="0" /></a></td>
我在FOR循环的初始化中猜测了它的某些内容,但是我在这里没有看到,但是对于前两个图像来说显然是错误的。这是脚本中的相关代码。
<?php
error_reporting(E_ALL);
require("config.php");
require("common.php");
$gallery = $_GET['page'];
$gallerydir = DOC_ROOT . GALLERY_ROOT . '/' . $gallery;
$imglist = getImageList($gallerydir);
$imgcnt = count($imglist);
$currpage = (isset($_GET['currpage']))
? (int) $_GET['currpage']
: 1;
// get page elements
$js = getJavaScript($imglist);
$thumb_table = getThumbTableHTML($gallery, $imglist, $imgcnt, $currpage);
$image_cnt = getImageRangeHTML($imgcnt, $currpage);
$page_nav = getPageNavHTML($imgcnt, $currpage, $gallery);
// load template header and body
$head = file_get_contents("./template/header.html");
$body = file_get_contents("./template/thumb_page.html");
// insert page elements
$head = str_replace('<-- JAVASCRIPT -->', $js, $head);
$body = str_replace('<-- THUMBNAIL_TABLE -->', $thumb_table, $body);
$body = str_replace('<-- IMAGE_COUNT -->', $image_cnt, $body);
$body = str_replace('<-- PAGE_NAVIGATION -->', $page_nav, $body);
// output
echo $head;
echo $body;
//*********************************************************************
// Begin thumbnail page related functions
//*********************************************************************
function getImageRangeHTML($imgcnt, $currpage) {
$max_per_page = TABLE_COLS * TABLE_ROWS;
$pagecnt = getThumbPageCount($imgcnt);
$first_image = ($currpage - 1) * $max_per_page + 1;
$last_image = ($currpage == $pagecnt)
? $imgcnt
: $currpage * $max_per_page;
$str = "
<font size=\"2\" face=\"Verdana, Geneva, sans-serif\"><i>
$imgcnt images
</i></font>
";
return $str;
}
//*********************************************************************
function getPageNavHTML($imgcnt, $currpage, $gallery) {
$pagecnt = getThumbPageCount($imgcnt);
$self = $_SERVER['PHP_SELF'];
// only proceed if there is more than 1 page of thumbs
if ($pagecnt == 1) { return; }
$str = "<ul>\n";
for ($i = 1; $i <= $pagecnt; $i++) {
if ($i == $currpage) {
$str .= "<li class=\"active\">$i</li>";
} else {
$str .= "<li><a href=\"javascript:imgSubmit('$self?";
$str .= "gallery=$gallery&currpage=$i');\">$i</a></li>";
}
}
$str .= "</ul>\n";
return $str;
}
//********************************************************************
function getThumbTableHTML($gallery, $imglist, $imgcnt, $currpage) {
$max_per_page = TABLE_COLS * TABLE_ROWS;
// set index of page's first image in image list array
$first_image_index = ($currpage - 1) * $max_per_page;
// set index of page's last image in image list array
$last_image_index = ($imgcnt < $currpage * $max_per_page)
? $last_image_index = $imgcnt
: $currpage * $max_per_page;
$str = '<table border="0" id="thumb_table">' . "\n";
// loop control var
$column_cnt = 1;
// loop through current page images
for ($i = $first_image_index; $i < $last_image_index; $i++) {
# build path to thumb
$thumburl = GALLERY_ROOT . "/$gallery/thumbs/" . $imglist[$i];
// insert row tag if this is the first column
if ($column_cnt == 1) { $str .= "<tr>\n"; }
// insert thumbnail, and surrounding html
$str .= "<td>";
$str .= "<a href=\"javascript:imgSubmit('image.php?gallery=$gallery";
$str .= "&imgnum=" . ($i + 1) . "');\">";
$str .= "<img src=\"$thumburl\" class=\"thumb_img\" ";
$str .= "border=\"0\" /></a>";
$str .= "</td>\n";
if ($column_cnt == TABLE_COLS) {
$str .= "</tr>\n\n";
$column_cnt = 1;
} else {
$column_cnt++;
}
}
$str .= "</table>\n";
return $str;
}
// *********************************************************************
function getThumbPageCount($imgcnt) {
$page_cnt = (int) (($imgcnt - 1) / (TABLE_COLS * TABLE_ROWS)) + 1;
return $page_cnt;
}
// *********************************************************************
?>
这是GetImageList()函数的相关代码
function getImageList($gallerydir) {
if (isset($_POST['imglist'])) {
$imglist = $_POST['imglist'];
} else {
$fh = opendir("$gallerydir/thumbs");
// read directory, and loop through retreived filenames
while ($file = readdir($fh)) {
// if image, add filename to image list
if (!preg_match("\.(jpg|gif|png|jpeg|jpe)$", $file)) {
$imglist[] = $file;
}
}
closedir($fh);
}
if (is_array($imglist)) {
ksort($imglist);
} else {
$imglist = false;
}
return $imglist;
}
//*********************************************************************
// Javascript writes image list as http posted array
function getJavaScript($imglist) {
$str = '
<script language="JavaScript">
function imgSubmit(s) {
document.img.method = "post";
document.img.action = s;
document.img.submit();
}
function imgList() {
';
$str .= "\tvar imglist = new Array('" . implode("', '", $imglist) . "');\n";
$str .= '
for (var i=0; i < imglist.length; i++) {
document.write ("<input type=\"hidden\" name=\"imglist[]\" ");
document.write ("value=\"" + imglist[i] + "\" />");
}
}
</script>
';
return $str;
}
//*********************************************************************
?>