<?php
/*
LGR Photo
Version .1.1
Feb. 26, 2004
This script is written by Lee Robertson. Feel free to use it/modify it as you see fit.
Use of this script as it our own risk. I make no guarantees that it will work on your machine/server.
If something goes wrong, it is your responsibility not mine.
All that being said, this script is just a quick image gallery script that I wrote because I could find
nothing out there that was as simple as just placing the script in the same folder as the images.
The script should work on most servers that have PHP installed. It was developed on a Windows machine with Apache and PHP.
A production version runs on a Linux machine with Apache and PHP.
If you like the script please either drop me a note at lelandrobertson@yahoo.com or
even better a dollar or two (or more) via Paypal is always nice.
If you would like this script modified to do something similar feel free to contact me.
I am sure we can come to some arrangement.
If you have other PHP work that you would like done, please drop me a note at lelandrobertson@yahoo.com.
INSTALL
You can provide configuration varibles to change how the script works.
If you don't tinker with any of the config variables it should work fine with the defaults.
Just place this script in the same folder as the JPEG images you want to have in your gallery.
If you want more albums, the script looks for images in subfolders below it, but it only does one level.
For example:
It will create galleries for all of the following folders:
/photos -> Main album
/photos/holidays ->Album
/photos/cats ->Album
BUT not for the following folder:
/photos/holidays/Saskatoon
The name of the subfolder automatically becomes the name of the album.
The first time you run the script it will create the thumbnails for you and create the gallery.
If you ever want to change the size of the thumbnails just change the value in the script and it
will recreate all of the thumbnails for you the next time you run the script.
**NOTE: Creating and recreating the thumbnails can take time. Please be patient. **
Simple eh!
Please leave this header alone.
*/
//Configuration
//Title for your gallery that will be used with the default header.
$title="";
//Thumbnail size. The minimum width and height in pixels, that you would like the thumbnails to be. Photos are scaled.
$twidth=200;
$theight=200;
//Photo size. The minimum width and height in pixels, that you would like you photos scaled to when displayed.
$pwidth = 1024;
$pheight = 768;
//The thumbnail gallery is displayed in a table. Please choose the number of rows and columns you would like.
$rows=5;
$cols=2;
//If you would like the filename of the photo to be displayed under the thumbnail change this setting.
//0=off 1=on
$showfilename=1;
//If you would like photo to up a new window when clicked change this setting.
//The defualt is to open the image up in the same browser window.
//0=off 1=on
$window=1;
//If you would like to use a custom header or footer please add the file names here and they will be included in the script.
//Otherwise the plain jane default will be used. The files should be in the same folder as the script, but if you provide an
//alternate path it will work as well. Example: header.html, header.php.
$header="NULL";
$footer="NULL";
//No need to mess with anything below here.
//Part of this function comes from
http://www.php.net/manual/en/function.getimagesize.phpfunction thumb_maker($filename, $minwidth, $minheight) {
if (file_exists($filename."thmb.jpg")) {
$photosize = getimagesize($filename."thmb.jpg");
if (max($minwidth,$minheight)!=max($photosize[0],$photosize[1])) {
unlink($filename."thmb.jpg");
}
}
if (!file_exists($filename."thmb.jpg") && file_exists($filename)) {
echo "";
set_time_limit(60);
$photosize = getimagesize($filename);
// Get image size and scale ratio
$scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
if ($scale < 1) {
$width = floor($scale*$photosize[0]);
$height = floor($scale*$photosize[1]);
}
else {
$width = $minwidth;
$height = $minheight;
}
if ($photosize[mime]=="image/jpeg") {
$resizedimage = imagecreatetruecolor($width, $height);
$thumbimage = imagecreatefromjpeg($filename);
imagecopyresampled($resizedimage, $thumbimage, 0, 0, 0, 0, $width, $height, $photosize[0], $photosize[1]);
imagejpeg($resizedimage,$filename."thmb.jpg",50);
imageDestroy($resizedimage);
imageDestroy($thumbimage);
}
}
}
//Part of this function comes from
http://www.php.net/manual/en/function.getimagesize.phpfunction gallery_sizer($photo,$minwidth,$minheight) {
if (file_exists($photo)) {
$photosize = getimagesize("$photo");
# Get image size and scale ratio
$scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
if ($scale <= 1) {
$width = floor($scale*$photosize[0]);
$height = floor($scale*$photosize[1]);
}
return array($width,$height,$photosize[0],$photosize[1]);
}
}
function getdirs($dir) {
$dirs=array();
chdir($dir);
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".."){
if (is_dir($file)) {
$dirs[] = $file;
}
}
}
closedir($handle);
}
sort($dirs);
return $dirs;
}
function getphotos($photodir) {
$photos=array();
if ($handle = opendir($photodir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !eregi(".jpgthmb.jpg$",$file) && eregi(".jpg$",$file)){
$photos[] = $file;
}
}
closedir($handle);
}
sort($photos);
return $photos;
}
function thumb_gallery($photonum) {
global $photos, $photourl, $photodir, $twidth, $theight, $rows, $cols, $showfilename, $album, $window;
if (($photonum)>(count($photos)-($rows*$cols))) {
$photonum=(count($photos)-($rows*$cols));
}
if (($photonum)<=0) {
$photonum=0;
}
if(isset($album)) { $query="&album=".urlencode($album); }
for ($tr = 1; $tr <= $rows; $tr++) {
$photobody[]="<tr>";
for ($td = 1; $td <= $cols; $td++) {
$photobody[]="<td align=\"center\">";
$size=gallery_sizer($photodir."/".$photos[$photonum]."thmb.jpg",$twidth,$theight);
$link = $photourl."/".$photos[$photonum]."thmb.jpg";
if ($window==1) { $target="_blank"; } else { $target="_self"; }
if ($photonum<=count($photos)-1) { $photobody[]="<a href=\"$_SERVER[PHP_SELF]?p=$photos[$photonum]$query\" target=\"$target\"><img src=\"$link\" width=\"$size[2]\" height=\"$size[3]\" alt=\"$photos[$photonum]\" border=0></a><br />"; }
if ($showfilename==1) { $photobody[]=$photos[$photonum]; }
$photobody[]="</td>";
$photonum++;
}
$photobody[]="</tr>";
}
unset($tr, $td);
#this id down here for a reason don't move it and use array_unshift instead. Trust me.
if (($photonum-($rows*$cols))>0) { $prev="<a href=\"$_SERVER[PHP_SELF]?pn=".($photonum-(($rows*$cols)*2)).$query."\"><< Tilbage</a>"; }
if (($photonum)<(count($photos))) { $next="<a href=\"$_SERVER[PHP_SELF]?pn=".$photonum.$query."\">Frem>></a>"; }
$photopage=array();
$photopage[]="Sider: ";
for ($pagelink=0, $pagenum=1; $pagelink<=count($photos); $pagelink+=($rows*$cols), $pagenum++) {
$photopage[]="<a href=\"$_SERVER[PHP_SELF]?pn=".($pagelink).$query."\">$pagenum</a> | ";
}
unset($pagelink, $pagenum);
$photopage=implode("", $photopage);
$photobody[]="<tr><td colspan=".$cols."><table width=\"100%\" border=\"0\"><tr><td width=\"33%\"><div align=\"LEFT\">".$prev."</div></td><td width=\"33%\"><div align=\"CENTER\" class=\"caption\">Billede <strong>".($photonum-(($rows*$cols)-1))."</strong> til <strong>".$photonum."</strong> ud af <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width=\"33%\"><div align=\"RIGHT\">".$next."</div></td></tr></table></td></tr>";
array_unshift($photobody, "<tr><td colspan=".$cols."><table width=\"100%\" border=\"0\"><tr><td width=\"33%\"><div align=\"LEFT\">".$prev."</div></td><td width=\"33%\"><div align=\"CENTER\">Billeder <strong>".($photonum-(($rows*$cols)-1))."</strong> til <strong>".$photonum."</strong> af <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width=\"33%\"><div align=\"RIGHT\">".$next."</div></td></tr></table></td></tr>");
#put the table tag at the top of the array.
array_unshift($photobody, '<table width="100%" border="0" cellspacing="0" cellpadding="3">');
$photobody[]='</table>';
return $photobody;
}
function photo_gallery($photo, $pwidth, $pheight) {
global $photodir, $photourl, $thumbsurl, $window;
$photobody=array();
if (isset($photo)) {
$size=gallery_sizer("$photodir/$photo",640,640);
$photobody[]='<table width="100%" border="0" cellspacing="0" cellpadding="3">';
$photobody[]='<tr><td colspan="3" align="center"></td></tr>';
if ($window==1) { $target="<a href=\"java script:window.close()\">Luk vinduet</a>"; } else { $target="<a href=\"".$_SERVER[HTTP_REFERER]."\">Tilbage</a>"; }
$photobody[]="<tr class=\"imageborder\"><td>".$target."</td><td align=\"center\"><div align=\"CENTER\" class=\"caption\"></div></td><td align=\"right\"> </td></tr>";
$photobody[]='<tr><td colspan="3" align="center">';
$photobody[]="<img src=\"$photourl/$photo\" width=\"$size[0]\" height=\"$size[1]\" border=1 alt=\"\" class=\"imageborder\">";
$photobody[]='</td></tr>';
$photobody[]="<tr class=\"imageborder\"><td align=\"center\" colspan=\"3\"><div align=\"CENTER\" class=\"caption\"></div></td></tr>";
$photobody[]="<tr class=\"imageborder\"><td>".$target."</td><td align=\"center\"><div align=\"CENTER\" class=\"caption\"></div></td><td align=\"right\"> </td></tr>";
$photobody[]='</table>';
}
return $photobody;
}
function display($photobody) {
global $photodir;
getheader();
albums($photodir);
foreach ($photobody as $value) {
echo $value."\n";
}
getfooter();
exit;
}
function albums($dir) {
//for now only one level of albums works.
global $album, $title;
$dirs=getdirs($dir);
if (isset($album)) { echo "<h1 align=\"center\">".$album."</h1>"; } else { echo "<h1 align=\"center\">".$title."</h1>"; }
echo "<div align=\"center\">";
if (isset($album)) { echo "<a href=\"".$_SERVER[PHP_SELF]."\">$title</a>\n"; } else { }
if (count($dirs)>0) {
foreach ($dirs as $value) {
echo "<a href=\"".$_SERVER[PHP_SELF]."?album=".urlencode($value)."\">".$value."</a>\n";
}
}
echo "</div>";
}
//returns the header for the page. Checks for a user defined header.
function getheader() {
global $header, $title;
if (isset($header) && $header!="NULL" && is_file ($header)) {
require("$header");
}
else {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
echo '<html>';
echo '<head>';
echo "<title>$title</title>";
echo '</head>';
echo '<body>';
}
}
//returns the footer for the page. Checks for a user defined footer.
function getfooter() {
global $footer;
if (isset($footer) && $footer!="NULL" && is_file ($footer)) {
require("$footer");
}
else {
echo "</body>\n</html>";
}
}
$photodir= dirname($_SERVER['PATH_TRANSLATED']);
$photourl=dirname($_SERVER[PHP_SELF]);
$thumbsurl=dirname($_SERVER[PHP_SELF]);
$photobody=array();
if (isset($_GET[album])) {
$album = urldecode($_GET[album]);
$photodir=dirname(realpath($_SERVER['PATH_TRANSLATED']))."/".$album;
$photourl=dirname($_SERVER[PHP_SELF])."/".$album;
$thumbsurl=dirname($_SERVER[PHP_SELF])."/".$album;
}
//To make sure this works both on Linux and Win
$photodir = str_replace("\\", "/", $photodir);
//Get the jpegs from the folder.
$photos=getphotos($photodir);
//check to see if thumbnails are made if not it will make. Adds time to the processing of the script.
for ($i=0; $i<=count($photos)-1; $i++) {
thumb_maker($photodir."/".$photos[$i], $twidth, $theight);
}
if (count($photos)<=0) {
echo "No JPEG photos in the folder to display";
exit;
}
if (isset($_GET[pn])) {
$photonum = $_GET[pn];
$photobody=thumb_gallery($photonum);
}
elseif (isset($_GET[p])) {
$photo=$_GET[p];
$photobody=photo_gallery($photo,$pwidth, $pheight);
}
else {
$photobody=thumb_gallery(0);
}
//output it all to the browser.
display($photobody);
exit;
?>