0

Somebody asked me to created a PHP image gallery that will read the images out of a directory and then create thumbnails for the gallery. There is one directory with thumnails, and one directory with fullsize images.

I read the file names out of the /thumb/ directory and insert each file name as a value into the thumbArray. From there I echo out the values in thumbnail src (<img src="<?php echo $thumbArray[$i]; ?>" />) where $i is just a counter. So the thumbnail images are produced from the array but when you click on the thumbnail, it queries ?filename into the url. Using $_SERVER['QUERY_STRING'] I then read said query string and insert the query, (filename), into the the large <img src"<?php echo $_SERVER['QUERY_STRING']; ?>" />. That is limited though. As I can now not read the array, as a reference point, and can no longer point forwards or backwards in the array.

Am I making more sense now?

Please help...

    <?php 
    $i = 0;

    /* Large file name and thumbnail file name must match */
    /* Large image size = 480px x 300px */
    echo '<img class="frameImg" src="images/large/'.$_SERVER['QUERY_STRING'].'" />';

    ?>
    <p id="prevNext"><a href="#">&lt;&lt; Prev </a> || <a href="#"> Next &gt;&gt;</a></p>
  </div>
  <div id="thumbs">
    <ul>
     <?php
    /* Must change $dir to the full path of directory all the way from root /home/user/domain/images/thumb */
    $dir = "*************************";
    $dh = opendir($dir);

    /* Thumbnail file name and large file name must match */
    $thumbArray = array();
    while (($file = readdir($dh)) !== false) {
        if ($file != "." && $file != "..") {
            $thumbArray[$i]=$file;
            echo '<li id="'.$i.'"><a href="?'.$thumbArray[$i].'"><img src="images/thumb/'.$thumbArray[$i].'" alt="Alt for '.$thumbArray[$i].'" /></a></li>';
            $i++;
        }
    }
    closedir($dh);
    ?>
4

1 回答 1

1

如果我理解正确,您只需要从 $thumbArray[$i] 数组中选择下一个和上一个值。

例如。

$prevThumb = $thumbArray[($i-1)];
$nextThumb = $thumbArray[($i+1)];

当然,对于第一个和最后一个,您必须格外小心。(并检查元素的存在)


编辑:好的,我明白了。

重新排列您的代码。你必须在每一页上阅读你的目录。那么为什么不在代码顶部将它读入一个数组,然后循环遍历这个数组来构建缩略图列表。

对于上一个/下一个按钮,您现在可以使用已构建数组中的索引来查找上一个/下一个图像的文件名。

我让自己明白了吗?

于 2009-08-21T22:30:54.817 回答