I currently run a website at http://www.solsblog.com . It is used to display my photography. I have a lot of knowledge of HTML but only so much of PHP. Some time ago I updated the site to have a script that would read my directories, make thumbnails, then link the thumbnails to the full image.
You can see how it works here:
http://www.solsblog.com/img/photodir.ph ... 6/03-24-06
A lot of people have complained that it is very hard to view my images because they have to click "Back" and then click the next thumbnail on the page.
I want to add a function that will make a "next" and "previous" link when each full image is displayed. However, my knowledge of PHP does not extend that far.
When I set up the site, I took a script I found online and chopped it apart to adapt to my site.
This is the script I use:
It is a file called photodir.php
Code: Select all
/* Directory where photos are stored */
$photo_dir = "/home/virtual/site91/fst/var/www/html/img/shots";
/* Directory for cached thumbnails */
$cache_dir = "/home/virtual/site91/fst/var/www/html/photos-cache";
/* URI to where photos are stored */
$photo_url = "/img/shots";
/* 'Frame' picture - thumbnails will be centred on top of this image */
$frame_pic = "outline.jpg";
/* 'Folder' picture */
$folder_pic = "folder.jpg";
/* 'Parent' picture */
$parent_pic = "folder.jpg";
/* Maximum dimensions for a thumbnail will be $thumb_max * $thumb_max. Thumbs
are made proportionatly, eg a 200x100 image with thumb_max of 100 will be
resized to 100x50. */
$thumb_max = 75;
/* *********************************************** */
/* END CONFIGURATION */
/* *********************************************** */
if ($_GET['photofile']) {
/* If we're just displaying a photo, send it out to the user. */
$photo = $photo_dir . "/" . stripslashes($_GET['photofile']);
if (realpath($photo) != $photo) {
/* Use realpath to find out if someone has tried spoofing us with
../../../../etc/passwd or similar. */
exit;
}
header("Content-type: image/jpeg");
readfile($photo);
exit;
} elseif ($_GET['thumbfile']) {
/* Display a thumbnail. This will be a resized version of the full file,
but superimposed on your outline image. */
$photo = $photo_dir . "/" . stripslashes($_GET['thumbfile']);
if (realpath($photo) != $photo) {
exit;
}
header("Content-type: image/jpeg");
$md5sum = md5("$photo");
$cache_file = $cache_dir . "/$md5sum";
if ((!file_exists($cache_file)) ||
(filemtime($cache_file) < filemtime($photo))) {
$outline_img = imagecreatefromjpeg($photo_dir . "/" . $frame_pic);
$outline_width = imagesx($outline_img);
$outline_height = imagesy($outline_img);
$src_img = imagecreatefromjpeg($photo);
$size = getimagesize($photo);
$origw = $size[0];
$origh = $size[1];
if ($origw > $origh) {
$neww = $thumb_max;
$diff = $origw / $neww;
$newh = $origh / $diff;
} else {
$newh = $thumb_max;
$diff = $origh / $newh;
$neww = $origw / $diff;
}
$dst_img = imagecreatetruecolor($neww, $newh);
if (function_exists('imagecopyresampled')) {
imagecopyresampled($dst_img,$src_img,0,0,0,0,$neww,$newh,
$origw,$origh);
} else {
imagecopyresized($dst_img, $src_img,0,0,0,0,$neww,$newh,$origw,
$origh);
}
imagejpeg($dst_img, $cache_file);
}
readfile ($cache_file);
exit;
}
include "header.html";
/* $dir is the full filesystem directory that we're looking at, eg
/var/httpd/photos/Holidays/NewYork2003
$path is everything AFTER the 'standard' photo directory, eg
Holidays/NewYork2003 */
if ($_GET['dir'] != "") {
$dir = $photo_dir . "/" . stripslashes($_GET['dir']);
$path = $_GET['dir'];
} else {
$dir = $photo_dir;
$path = "";
}
if (substr($dir, -1, 1) == "/") {
/* Remove the trailing slash if there is one */
$dir = substr($dir, 0, -1);
}
if (substr($path, -1, 1) == "/") {
/* Remove the trailing slash if there is one */
$path = substr($path, 0, -1);
}
if ($dir != realpath($dir)) {
/* Quick check to make sure our path hasn't been
poisened, eg ../../../../etc/passwd or similar. */
exit;
}
/* Initialise basic variables */
$i = 0;
$first = "y";
if ((is_dir($dir)) && ($dir != "") && ($_GET['image'] == "")) {
if ($dh = opendir($dir)) {
echo "<table border=0 width=680>";
echo "<tr>";
while (($file = readdir($dh)) !== false) {
if (($file != ".") && ($file != "..") &&
!(($file == $folder_pic) && ($path != "/")) &&
!(($file == $frame_pic) && ($path != "/"))) {
if (eregi("jpg", $file)) {
/* This is an image, display its thumbnail and a link
to the full image. */
echo "<td width=85 align=center valign=center>";
echo "<a href='$PHP_SELF?image=$path/$file'>";
echo "<img src='$PHP_SELF?thumbfile=";
echo $path . "/" . $file;
echo "' border=0><br>";
echo "</a><br>";
echo "</td>\n";
$i++;
}
if ($i == {
echo "</tr>\n<tr>\n";
$i = 0;
}
}
}
closedir($dh);
echo "</tr></table>";
} else {
echo "That directory can't be opened! <br>";
echo "Maybe try checking permissions.<br>";
}
} else {
/* Display large photo and information */
$photo = $photo_dir . "/" . $_GET['image'];
echo "<p align=center>";
echo "<img src='$PHP_SELF?photofile=" . stripslashes($_GET['image']) . "'>";
echo "<br>";
$parts = explode("/", stripslashes($_GET['image']));
if (count($parts) == 0) {
$parent = "";
} else {
for ($j=0; $j<count($parts)-1; $j++) {
$parent .= $parts[$j] . "/";
}
$parent = substr($parent, 0, -1);
}
$exif = exif_read_data($photo, 0, true);
// TEST CAMERA AND PRINT
$cameratest = $exif['IFD0']['Model'];
if ($cameratest != null){
print "Camera: " . $exif['IFD0']['Model'] . "<br/>";
} else {
print "Camera: N/A <br/>";
}
// TEST APERTURE AND PRINT
$apertest = $exif['COMPUTED']['ApertureFNumber'];
if ($apertest != null) {
echo "Aperture: " . $exif['COMPUTED']['ApertureFNumber'] . "<br/>";
} else {
echo "Aperture: N/A <br/>";
}
// TEST ISO AND PRINT
$isotest = $exif['EXIF']['ISOSpeedRatings'];
if ($isotest != null) {
echo "ISO: " . $exif['EXIF']['ISOSpeedRatings'] . "<br/>";
} else {
echo "ISO: N/A <br/>";
}
// TEST EX AND PRINT
$extest = $exif['EXIF']['ExposureTime'];
if ($extest != null) {
echo "Exposure Time: " . $exif['EXIF']['ExposureTime'] . " sec <br/>";
} else {
echo "Exposure Time: N/A <br/>";
}
// TEST FOCAL LENGTH AND PRINT
$focaltest = $exif['EXIF']['FocalLength'];
if ($focaltest != 0) {
$focal = explode("/", $exif['EXIF']['FocalLength']);
$focal = $focal[0] / $focal[1];
print "Focal Length: " . round($focal) . " mm <br/>";
} else {
print "Focal Length: N/A <br/>";
}
echo "<br>";
echo "<a href='$PHP_SELF?dir=$parent'>Back to Gallery</a>";
echo "</p>";
}
include "footer.html";
function format_file_size($size) {
if ($size <= 1024) {
return $size . "Mb";
} elseif (($size > 1024) && ($size <= 1024000)) {
$size = $size / 1024;
$size = round($size, 2);
return $size . "Kb";
} elseif (($size > 1024000) && ($size <= 1024000000)) {
$size = $size / 1024000;
$size = round($size, 2);
return $size . "Mb";
} else {
$size = $size / 1024000000;
$size = round($size, 2);
return $size . "Gb";
}
}