tags:

views:

793

answers:

2

Hi,

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);
 ?>

http://client.clogg.ca/janelle-nadeau/gallery.php

A: 

I'm not quite sure if I understand.

If the large images are shown based on the URL of the thumb nails, then only large images will show that have a corresponding thumb nail URL in you list. No thumb nail, no large. Correct?

Robert DeBoer
The thumnail and large have identical names. The href on the thumbnail link puts ?filename in the url and then the large image reads that query and puts that filename in the src of the large image
Vian Esterhuizen
So there are an uneven number of large and small images.
Robert DeBoer
Sorry. I didn't realize I'm being so unclear. I wish I could explain this properly. There is the same amount of images in "/images/thumb/" as there are in "/images/large/" the corresponding "thumb" and "large" have identical file names. So I can reference them easier.You were correct in your original question, I didn't quite understand the first time I read it. Sorry.
Vian Esterhuizen
+1  A: 

if i understand you correctly you just need to select the next and prev values from your $thumbArray[$i] Array.

eg.

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

of course for first and last you must take extra care. (and checking the existent of the element)


EDIT: ok i understand.

Rearange your code. you have to read your directory on every page. so why not read it at top of your code into an array, and loop through this array to build the thumbnail list.

for the prev/next buttons you can now use the index from your already build array to find the filename for the prev/next image.

did i make myself understandble ?

Rufinus
Thank you. I guess I wan't clear. The next and prev buttons are supposed to be for the Large image. As it stands now the large file isn't reading from the array, it's just reading from the url, so I'm unable to do what you said.Essentially what I'm having trouble with is getting the large image to read from the array...
Vian Esterhuizen