Any good PHP-based photo-gallery apps?

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Darth Wong
Sith Lord
Sith Lord
Posts: 70028
Joined: 2002-07-03 12:25am
Location: Toronto, Canada
Contact:

Any good PHP-based photo-gallery apps?

Post by Darth Wong »

Having looked around for one that was simple and easy to update, I eventually got fed up and made my own.

http://www.stardestroyer.net/Mike/Family/

Nothing complicated; it just parses some directory trees, looking for JPEG files and then assembles webpages out of them. If it finds a file with the same name and a .txt suffix, it uses that file as the caption. If it finds a file with the same name and a _THM.JPG suffix, it treats it as a thumbnail and displays it while linking to the real image. I don't have much experience optimizing PHP for performance though, and I don't know how optimal this solution is. Any PHP experts in the house? The code looks like this:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
<?
	if ($imagedir) $maindir = preg_replace("/^\/|\.+/",".",addslashes("$imagedir"));
	else $maindir = "Main";
	if (!file_exists($maindir) && !file_exists("../PhotoAlbum/$maindir")) $maindir = "Main";
	echo "	<TITLE>The Wong Clan Photo Album - $maindir</TITLE>\n";
?>
	<META NAME="AUTHOR" CONTENT="Michael Wong">
	<LINK REL="STYLESHEET" TYPE="text/css" HREF="../style.css">
</HEAD>
<BODY>
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=4 CELLSPACING=0>
	<TR>
		<TD WIDTH=125>
			<A HREF="../index.html"><IMG SRC="../turhome.gif" ALIGN=LEFT WIDTH=30 HEIGHT=30 BORDER=0 ALT="Home"><B>Home</B></A>
		</TD>
		<TD COLSPAN=4>
<?
	echo "			<H1 ALIGN=CENTER STYLE=\"margin-bottom:0in\">The Wong Clan Photo Album - $maindir</H1>\n";
	echo "			<P ALIGN=CENTER STYLE=\"margin-top:0in\">";
	if (!file_exists($maindir))
		echo "Click on images to enlarge";
	else
		echo "&nbsp;";
	echo "</P>\n";
?>
		</TD>
	</TR>
	<TR VALIGN=TOP>
		<TD WIDTH=125>
			<HR>
<?
echo "			<P><B><A HREF=\"$PHP_SELF?imagedir=Main\">Main</A></B></P>\n";
echo "			<P><B><A HREF=\"$PHP_SELF?imagedir=Matthew\">Matthew</A></B></P>\n";
echo "			<P><B><A HREF=\"$PHP_SELF?imagedir=David\">David</A></B></P>\n";
echo "			<P><B><A HREF=\"$PHP_SELF?imagedir=Fuzzy\">Fuzzy</A></B></P>\n";
?>
			<HR>
<?
$readdir = "../PhotoAlbum";
$mydir = opendir($readdir);
while($fn = readdir($mydir))
	{
		if (filetype("$readdir/$fn") == "dir" && $fn != "." && $fn != "..")
			$dir_array[] = $fn;
	}
closedir($mydir);
if (count($dir_array) > 0) sort($dir_array);
for ($i=0; $i < count($dir_array); $i++)
	echo "			<P><B><A HREF=\"$PHP_SELF?imagedir=$dir_array[$i]\">$dir_array[$i]</A></B></P>\n";
?>
			<HR>
		</TD>
		<TD WIDTH=20 STYLE="background:black">&nbsp;</TD>
		<TD VALIGN=TOP STYLE="background:black">
<?
if (file_exists($maindir))
	{
	$mydir = opendir($maindir);
	$side = "left";
	while($fn = readdir($mydir))
		{
			if ( strstr($fn,'jpg') )
				$file_array[] = $fn;
		}
	closedir($mydir);
	
	if (count($file_array) > 0) sort($file_array);
	for ($i=0; $i < count($file_array); $i++)
		{
		echo "			<TABLE STYLE=\"background:black\">\n";
		echo "			<TR><TD COLSPAN=2 STYLE=\"background:black\">&nbsp;</TD></TR>\n";
		echo "			<TR VALIGN=MIDDLE><TD STYLE=\"background:black;color:white\">";
		if ( $side == "right" )
			{
			echo "<IMG SRC=\"$maindir/$file_array[$i]\" BORDER=1 ALT=\"Image\"></TD>\n";
			echo "<TD STYLE=\"background:black;color:white\">";
			}
		$textfile=str_replace('.jpg','.txt',$file_array[$i]);
		if (file_exists("$maindir/$textfile"))
			{
			$textopen=fopen("$maindir/$textfile", "r");
			$textdata=preg_replace("/\n\n/","<BR><BR>",htmlspecialchars(fread($textopen, filesize("$maindir/$file_array[$i]"))));
 			fclose($textopen);
			echo $textdata;
			}
		echo "</TD>";
		if ( $side == "left" )
			{
			echo "			<TD STYLE=\"background:black;color:white\">";
			echo "<IMG SRC=\"$maindir/$file_array[$i]\" BORDER=1 ALT=\"Image\"></TD>";
			}
		echo "</TR>\n";
		echo "			<TR><TD COLSPAN=2 STYLE=\"background:black\">&nbsp;</TD></TR>\n";
		echo "			</TABLE>\n";
		if ( $side == "right" )
			$side = "left";
		else if ( $side == "left" )
			$side = "right";
		}
	}
else
	{
	$altdir = "../PhotoAlbum/$maindir";
	$mydir = opendir($altdir);
	$dir_array2[] = ".";
	while($fn = readdir($mydir))
		{
			if (filetype("$altdir/$fn") == "dir" && $fn != "." && $fn != "..")
				$dir_array2[] = $fn;
		}
	closedir($mydir);
	sort($dir_array2);
	echo "			<TABLE STYLE=\"background:black\">\n";
	for ($x=0; $x < count ($dir_array2); $x++)
		{
			$photodir = "$altdir/$dir_array2[$x]";
			$mydir = opendir($photodir);
			//for ($i=0; $i < count($file_array); $i++)
			//	{
			//		unset ($file_array[$i]);
			//		array_pop($file_array);
			//	}
			unset($file_array);
			while($fn = readdir($mydir))
				{
					if ( stristr($fn,'jpg') && !strstr($fn,'_THM.JPG'))
					$file_array[] = $fn;
				}
			closedir($mydir);
			if (count($file_array) > 0)
				{
				sort($file_array);
				for ($i=0; $i < count($file_array); $i++)
					{
					$thumbfile=str_replace('.jpg','_THM.JPG',str_replace('.JPG','_THM.JPG',$file_array[$i]));
					$textfile=str_replace('.jpg','.txt',str_replace('.JPG','.txt',$file_array[$i]));
					echo "			<TR>";
					echo "<TD COLSPAN=2 STYLE=\"background:black\">&nbsp;</TD></TR>\n";
					echo "			<TR VALIGN=MIDDLE>\n";
					echo "			<TD WIDTH=132 STYLE=\"background:black;color:white\" ALIGN=CENTER>";
					echo "<A HREF=\"$photodir/$file_array[$i]\" TARGET=\"_blank\">";
					echo "<IMG SRC=\"$photodir/$thumbfile\" BORDER=0 ALT=\"Image\"></A></TD>\n";
					echo "			<TD STYLE=\"background:black;color:white\">";
					if (file_exists("$photodir/$textfile"))
						{
						$textopen=fopen("$photodir/$textfile", "r");
						$textdata=preg_replace("/\n\n/","<BR><BR>",htmlspecialchars(fread($textopen, filesize("$photodir/$file_array[$i]"))));
 						fclose($textopen);
						echo $textdata;
						}
					else
						{
						echo "<SPAN STYLE=\"font-size:10px;color:gray\">";
						echo "$dir_array2[$x]/$file_array[$i]</SPAN>";
						}
					echo "</TD>\n";
					echo "			</TR>\n";
					}
				}
		}
		echo "			<TR><TD COLSPAN=2 STYLE=\"background:black\">&nbsp;</TD></TR>\n";
		echo "			</TABLE>\n";
	}
?>
		</TD>
		<TD WIDTH=20 STYLE="background:black">&nbsp;</TD>
	</TR>
</TABLE>
<? include ('../Footer.inc') ?>
It parses the current directory as well as another directory elsewhere in the tree called "PhotoAlbum". But I didn't want to bother creating a database, so I'm hoping that it's not really inefficient for PHP to parse the directory tree like that. Can anyone offer insight as to the efficiency of PHP's directory navigation?
Image
"It's not evil for God to do it. Or for someone to do it at God's command."- Jonathan Boyd on baby-killing

"you guys are fascinated with the use of those "rules of logic" to the extent that you don't really want to discussus anything."- GC

"I do not believe Russian Roulette is a stupid act" - Embracer of Darkness

"Viagra commercials appear to save lives" - tharkûn on US health care.

http://www.stardestroyer.net/Mike/RantMode/Blurbs.html
User avatar
Pu-239
Sith Marauder
Posts: 4727
Joined: 2002-10-21 08:44am
Location: Fake Virginia

Post by Pu-239 »

I have no clue... my setup is to manually run a perl script every time an image is added (cronjob may do), to generate static galleries, and it also generates the thumbnails using perlmagick- which solves the problem of performance- of course if you are hoping to dynamically specify how many per page and whatnot, this will not work. I'm not big w/ PHP or perl though, since my friend did most of it (just tweaked it to make the generated code more standards compliant).

ah.....the path to happiness is revision of dreams and not fulfillment... -SWPIGWANG
Sufficient Googling is indistinguishable from knowledge -somebody
Anything worth the cost of a missile, which can be located on the battlefield, will be shot at with missiles. If the US military is involved, then things, which are not worth the cost if a missile will also be shot at with missiles. -Sea Skimmer


George Bush makes freedom sound like a giant robot that breaks down a lot. -Darth Raptor
Post Reply