PHP Folder Gallery
Written on December 6, 2009 at 11:31 pm, by Harlan Brown
I stumbled upon a nice little php script to create a gallery from a folder. The script worked for all intensive purposes, but was not XHTML 1.0 Transitional Compliant. So, like the enthusiast I am, I set out to make it as such.
This is a php script which will parse through a folder, grab anything in that folder, assign it a table cell, attempt to make it the background image of a div inside that cell, and put a 15% transparent white image in the div in order to create a link to the image. The link opens the image in a new browser / tab instance.
PHP / XHTML:
<?php
//init counter
$a = '0';
//new row variable
$newrow = "</tr><tr>";
//sets the directory where your images are to be pulled from
$filepath = "gallery"; //your image folder
$dir = dir($filepath);
//start the table for the gallery
echo '<table id="gal1" border="0" cellpadding="2" cellspacing="2" width="75%"><tr>';
//while there are files keep reading the directory
while($entry=$dir->read()) {
if($entry == "." || $entry == ".." || strtoupper($entry) == "THUMBS.DB") {
continue;
}
$fp = @fopen("$filepath/$entry","r");
//dynamically adds a new row once a multiple of 5 is reached
if ($a % 5 == 0 && $a != 0){echo $newrow;}
//replaces the space in the URI with the right formatting of %20
$entry = str_replace(' ', '%20', $entry);
//code for the indvidual images in the gallery
echo '
<td>
<div class="thumb" style="background-image: url('.$filepath.'/'.$entry.');">
<a target="_blank" href="'.$filepath.'/'.$entry.'">
<img src="img/gallery-link.png" alt="'.$entry.'" />
</a>
</div>
</td>';
//counter
$a++;
}
echo '</tr>
</table>';
?>
CSS:
.thumb{width: 125px; height: 125px; background-position: left; background-repeat: no-repeat; border: 1px solid #666666;}
.thumb a img{border: none;}
#gal1{margin-left: auto; margin-right: auto;}
Example: phpFolderGallery
Download: phpFolderGallery.zip
Original: Creating an Image Gallery with PHP

