Jump to content

picture gallery help


busnut

Recommended Posts

G'day guys/gals

 

I have this very very basic picture gallery on my site, but I need it to do 3 things that is beyond my knowledge (again).

 

The following script is a script i've found on the net, and have made slight mods to it.

 

<h1>Photo Gallery</h1>

<div align="center">

<table width="750"><tr><td align=center>
<?php
// define directory path
$dir = "gallery";

// iterate through files
// look for JPEGs
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match("/.jpg/", $file)) {
                echo "<a href=$dir/$file rel=\"lightbox\"><img src=gallery-thumbnail.php?file=$file></a> ";
           }
        }
       closedir($dh);
   }
}
?>
</td></tr></table>
</div>

<?php
// define directory path
$dir = "gallery";
$image = exif_thumbnail($dir . "/" . $_GET['file']);
header("Content-Type: image/jpeg");
echo $image;
?> 

 

Firstly, I want the pictures to display in alphnumerical order (ie, a, b, c, 1, 2, 3 onwards), where at the moment it displays in some sort of order I can't work out, thought it was by date modified but it isn't.

Secondly, I want it to display the 'comment' data from the exif of the photo, and the best I can get is just the filename and some other useless info, but not the comment data.

If by chance this isn't possible, is there a way that it will read a txt file and say if image 101.jpg is displayed, it will show info about it?

And thirdly as something different, if I want it to search through subfolders of the gallery folder, how can this be done so that it will either show all the images in the subfolder too or alternatively display a link where once clicked, will then show the images.

 

Why i've opted for this approach is all i have to do is upload the image to the image directly and it does the rest by itself...More so simple for those who have been granted to upload images without the need of all the special features of photo gallery scripts.

 

Any help is much appreciated!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/141327-picture-gallery-help/
Share on other sites

If you can use scandir() instead of opendir()and readdir(), then you can retrieve the directory entries sorted alphabetically by filename

I replaced both said as mentioned, and the page just wouldn't load, I even added a sort($file); command in there also, still no luck, but then reverted to just the sort and it displayed an error message above each picture.
Link to comment
https://forums.phpfreaks.com/topic/141327-picture-gallery-help/#findComment-739736
Share on other sites

scandir() requires PHP 5 or above, but should work something like this:

if (is_dir($dir)) {
   $files = scandir($dir);
   foreach ($files as $file) {
      if (preg_match("/.jpg/", $file)) {
         echo "<a href=$dir/$file rel=\"lightbox\"><img src=gallery-thumbnail.php?file=$file></a> ";
      }
   }
}

 

An alternative would be to build an array in your readdir() loop, sort that, and then iterate again to echo:

$files = array();
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
         if (preg_match("/.jpg/", $file)) {
            $files[] = $file;
         }
      }
      closedir($dh);
   }
}

sort($files);

foreach ($files as $file) {
   echo "<a href=$dir/$file rel=\"lightbox\"><img src=gallery-thumbnail.php?file=$file></a> ";
}

Link to comment
https://forums.phpfreaks.com/topic/141327-picture-gallery-help/#findComment-739743
Share on other sites

I've had some success in displaying comments, but have not used the exif data from the image, but rather from a txt file, which works perfectly, but for this hopefully what is the final stage for my gallery script, is how can I have the images in directories from the gallery directory? ie: when the gallery page displays, it looks at the 'gallery' directory and will display the subfolder names under the gallery directory, and then once a directory is selected, then shows my images.

 

Here is what I have so far:

 

<h1>Photo Gallery</h1>

<p>Below is a collection of photographs taken by our members of buses, events and displays!</p>
<div align="center">

<table><tr>
<?php
// define directory path
$dir = "gallery";
$text = "gallery.txt";
$cols = 5;
$colswidth = 20;

$fh=fopen($text, "r"); 

while(!feof($fh)) 
{ 
  $temp = explode(";", $line); 
  $description[$temp[0]] = $temp[1];
  $excompany[$temp[0]] = $temp[2];
  $photographer[$temp[0]] = $temp[3];
  $line=fgets($fh); 
  unset($temp); 
} 

if (is_dir($dir)) {
   $files = scandir($dir);

$colCtr = 0; 

   foreach ($files as $file) {

      if (preg_match("/.jpg/", $file)) {

  if($colCtr %$cols == 0) 
        echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>';

	$exif = exif_read_data($dir."/".$file, 0, true);
        echo "<td valign=top align=center width=". $colswidth ."%><a href=$dir/$file rel=\"lightbox\" title=\"$description[$file]<br>$excompany[$file]<br>© $photographer[$file]\"><img src=gallery-thumbnail.php?file=$file border=2 style='border-color: #981C1E'></a><br>$description[$file]</td>"; 
  $colCtr++; 
     }
   }
}
?>

</tr></table>
</div>

 

Link to comment
https://forums.phpfreaks.com/topic/141327-picture-gallery-help/#findComment-741870
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.