PHP : Image Gallery

17 Mar

<?php

/*

Created by Jeff Harrell
http://www.juxtadesign.com

Feel free to implement this however you want. However, if you
make any good changes then let me know and I will see about
incorporating them. Use at your own risk!

Just place PHP file in a folder that contains subfolders of images.
For formatting, the script will convert “_” characters in folder and
image names to spaces and title case the name. Make sure there are no
image files on the same level as the PHP script.

*/

/* CONFIGURABLE OPTIONS */

// page and breadcrumb prefix (okay to use html)
$title = ‘My Site | ‘;

// description (used in meta tags)
$description = ‘A collection of personal digital photos taken by you!’;

// the CSS is located in an external file named in the configuration below.
// if you would like to use the style sheet this was created with you may
// download it here:
// http://juxtadesign.com/photos/styles.css
$style_sheet = ‘styles.css’;

// allowed image extensions (separate by spaces)
$image_extensions = ‘jpg gif png’;

/* NO NEED TO CHANGE BELOW UNLESS YOU WANT TO */

// get the scripts info
$path = explode(‘/’,$_SERVER[‘SCRIPT_FILENAME’]);
$name = $path[(count($path) – 1)];
$folder = $path[(count($path) – 2)];
$show_directory = $_GET[‘directory’];
$show_image = $_GET[‘image’];
$keywords = ”;

function process() {
global $title, $path, $name, $folder, $show_directory, $show_image, $image_extensions;

// if url contains a directory go there
if ($show_directory) {
if (is_dir($show_directory)) {
chdir($show_directory);
} else {
// error directory does not exist!
header(‘Location: ‘ . $_SERVER[‘SCRIPT_NAME’]);
}
}

// now get the current directory
$cwd = getcwd();

// open directory
if ($handle = opendir($cwd)) {
// loop through all files adding files and directories to an array
while (false !== ($file = readdir($handle))) {
if (!preg_match(“/^\./”, $file)) {
$ext = explode(‘.’, $file);
$ext = $ext[count($ext) – 1];

if (is_dir($file)) {
$dirs[] = $file;
} else if (strpos($image_extensions, $ext) > -1) {
$files[] = $file;
}
}
}

// close directory
closedir($handle);

// process any files and folders found
processfolders($dirs);
processfiles($files, $dirs, $cwd);
}

return ob_get_contents();
}

function processfolders($dirs) {
// if directories exist sort and display them
if (is_array($dirs)) {

// output the page breadcrumb navigation
breadcrumb();

// sort the directories
natsort($dirs);
$dirs = array_values($dirs);

// output the directory listing
echo ‘<div class=”textlinks”>’;
foreach($dirs as $dir) {
echo ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $dir . ‘&image=0″ title=”‘ . formatname($dir) . ‘”> ‘ . formatname($dir) . ‘</a><br />’;
}
echo ‘</div>’;
}
}

function processfiles($files, $dirs, $cwd) {
global $title, $path, $name, $folder, $show_directory, $show_image, $keywords, $image_extensions;

// if no files exist then output some pictures
if (!is_array($files)) {
// open a random directory
srand((double) microtime() * 1000000);
$rand_dir = $dirs[rand(0,count($dirs)-1)];

if (is_dir($rand_dir)) {
chdir($rand_dir);

// open directory
if ($handle = opendir($cwd . ‘/’. $rand_dir)) {
// loop through all files
while (false !== ($file = readdir($handle))) {
// ignore hidden files and this file and add all else to an array
if (!preg_match(“/^\./”, $file)) {
$ext = explode(‘.’, $file);
$ext = $ext[count($ext) – 1];

if (!is_dir($file) AND strpos($image_extensions, $ext) > -1) {
$files[] = $file;
}
}
}
}
// close directory
closedir($handle);

// output multiple images in a block
echo ‘<div class=”imageblock”>’;
$x = 0;
while ($x < 4) {
// choose a random picture and get its heigh / width
// slightly buggy since it can sometimes add more than one of the same pic
$rand_pic = rand(0,count($files) – 1);
$image_info = getimagesize($files[$rand_pic]);
$image_width = $image_info[0];
$image_height = $image_info[1];

// if the picture format is landscape then use it
if ($image_width >= $image_height) {
echo ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $rand_dir . ‘&image=’ . $rand_pic . ‘” title=”‘ . formatname($files[$rand_pic]) . ‘”><img src=”‘ . $rand_dir . ‘/’ . $files[$rand_pic] . ‘” width=”160″ height=”120″ alt=”‘ . $files[$rand_pic] . ‘” /></a>’;
$x++;
}
}
echo ‘</div>’;
} else {
// output the page breadcrumb navigation
breadcrumb();

// output the directory listing
echo ‘<div class=”textlinks”>There are no files to display.</div>’;
}

// else if files exist in directory sort and display them
} else if (is_array($files)) {
// sort the files
natsort($files);
$files = array_values($files);

// url of image
// if none fix link color issue and redirect to image 0 (work around)
if (isset($show_image) AND is_file($files[$show_image])) {
$image = $files[$show_image];
} else {
header (‘Location: ‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $show_directory . ‘&image=0’);
}

// output the page breadcrumb navigation
breadcrumb();

// title of image
echo ‘<div class=”title”>’;
echo formatname($image);
echo ‘</div>’;

// image
if (file_exists($image)) {
$image_info = getimagesize($image);
$image_width = number_format($image_info[0]);
$image_height = number_format($image_info[1]);

echo ‘<div class=”photo”>’;
echo ‘<img src=”‘ . $_REQUEST[‘d
irectory’] . ‘/’ . $image . ‘” width=”‘ . $image_width . ‘” height=”‘ . $image_height . ‘” alt=”‘ . $image . ‘”>’;
echo ‘</div>’;
}

// next and previous links for the images
echo ‘<div class=”textlinks”>’;
if ($_REQUEST[‘image’] > 0) {
echo ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $_REQUEST[‘directory’] . ‘&image=’ . ($_REQUEST[‘image’] – 1) . ‘” title=”‘ . formatname($files[$_REQUEST[‘image’] – 1]) . ‘”>Previous</a>’;
} else {
echo ‘Previous’;
}

echo ‘ | ‘;

if ($_REQUEST[‘image’] < (count($files) – 1)) {
echo ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $_REQUEST[‘directory’] . ‘&image=’ . ($_REQUEST[‘image’] + 1) . ‘” title=”‘ . formatname($files[$_REQUEST[‘image’] + 1]) . ‘”>Next</a>’;
} else {
echo ‘Next’;
}
echo ‘</div>’;

// image links
// add image name to keywords variable
echo ‘<div class=”numberlinks”>’;
foreach($files as $file) {
$i++;
echo ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘?directory=’ . $_REQUEST[‘directory’] . ‘&image=’ . ($i – 1). ‘” title=”‘ . formatname($files[$i – 1]) . ‘”>’ . $i . ‘</a> ‘;
keywords($file);

}
echo ‘</div>’;
}
}

function formatname($string) {
global $image_extensions;

$string = ereg_replace(“_+”, ‘ ‘, $string);
$string = ereg_replace(“-+”, ‘ ‘, $string);
$string = ereg_replace(“\r+”, ‘ ‘, $string);
$string = ereg_replace(“\t+”, ‘ ‘, $string);
$string = ereg_replace(” +”, ‘ ‘, $string);

$image_extension = explode(‘ ‘, $image_extensions);
for($i=0;$i<count($image_extension);$i++) {
$string = str_replace(‘.’ . $image_extension[$i],”,$string);
}

$string = ucwords($string);
return $string;
}

function breadcrumb() {
global $title, $folder, $show_directory;

echo ‘<div class=”breadcrumb”>’;
if ($show_directory) {
echo $title .= ‘<a href=”‘ . $_SERVER[‘SCRIPT_NAME’] . ‘”>’ . formatname($folder) . ‘</a> | ‘ . formatname($show_directory);
} else {
echo $title .= formatname($folder);
}
echo ‘</div>’;
}

function keywords($string) {
global $keywords;

$words = formatname($string);
$words = explode(‘ ‘, $words);

foreach($words as $word) {
if (!empty($word)) {
if (strpos($keywords, $word) === false) {
$keywords .= $word . ‘, ‘;
}
}
}
}

// generate the photo html
ob_start();
$html = process();
ob_end_clean();

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en” lang=”en”>
<head>
<title><?php echo strip_tags($title); ?></title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<meta name=”description” content=”<?php echo $description; ?>” />
<meta name=”keywords” content=”<?php echo $keywords; ?>” />
<link rel=”stylesheet” type=”text/css” media=”screen” href=”<?php echo $style_sheet; ?>” />
</head>
<body>

<?php echo $html; ?>

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: