Skip to content

Instantly share code, notes, and snippets.

@neilisaac
Created April 14, 2013 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilisaac/5380750 to your computer and use it in GitHub Desktop.
Save neilisaac/5380750 to your computer and use it in GitHub Desktop.
Simple PHP photo album generated by searching for files in given folder
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Photo Albums</title>
<style>
body {
background-color: #222222;
color: #FFFFFF;
}
div.box {
float: left;
margin: 20px;
padding: 10px;
border: 1px solid #666666;
background-color: #444444;
text-align: center;
}
img.thumb {
border: 0;
max-width: 256px;
max-height: 256px;
}
</style>
</head>
<body>
<?php
$dir = htmlspecialchars($_GET['d']);
$gen = htmlspecialchars($_GET['g']);
print "<h2>albums" . ($dir ? "/$dir" : "") . "</h2>\n";
if ($gen) {
print "Generating thumbnails for <i>$gen</i>...<br/>\n";
if (!file_exists("thumbnails")) {
print "&nbsp;&nbsp;creating folder <i>thumbnails</i><br/>\n";
mkdir("thumbnails");
}
if (!file_exists("thumbnails/$gen")) {
print "&nbsp;&nbsp;creating folder <i>thumbnails/$gen</i><br/>\n";
mkdir("thumbnails/$gen");
}
try {
foreach (new DirectoryIterator("./albums/$gen") as $file) {
if ($file->isDot())
continue;
if ($file->isFile()) {
$name = $file->getFilename();
$image = str_replace(" ", "\ ", "albums/$gen/$name");
$thumbnail = str_replace(" ", "\ ", "thumbnails/$gen/$name");
if (!file_exists("thumbnails/$gen/$name")) {
print "&nbsp;&nbsp;generating thumbnail for <i>$name</i><br/>\n";
system("convert -geometry 256x256 $image $thumbnail");
}
}
}
print "Thumbnails have been generated for <a href=\"?d=$gen\">$gen</a><br/>\n";
}
catch (RuntimeException $ex) {
print "Error: " . $ex->getMessage() . "<br/>\n";
}
}
else {
try {
$iter = new DirectoryIterator("./albums/$dir");
foreach ($iter as $file) {
$name = $file->getFilename();
if ($file->isDot())
continue;
print "<div class=\"box\">";
if ($file->isDir()) {
print "<a href=\"?d=" . ($dir ? "$dir/$name" : $name) . "\">";
print "<img class=\"thumb\" src=\"folder.png\" alt=\"$name\"/>";
} else {
print "<a href=\"albums/$dir/$name\">";
$thumbnail = file_exists("thumbnails/$dir/$name") ? "thumbnails/$dir/$name" : "image.png";
print "<img class=\"thumb\" src=\"$thumbnail\" alt=\"$name\"/>";
}
print "</a><br/>$name</div>\n";
}
if ($dir) {
print "<div class=\"box\">";
print "<a href=\".\">";
print "<img class=\"thumb\" src=\"home.png\" alt=\"Go back\"/>";
print "</a><br/>Return to album list</div>\n";
print "<br style=\"clear:both\"/><a href=\"?&g=$dir\">Generate</a> thumbnails for this album\n";
}
}
catch (RuntimeException $ex) {
print "Error: " . $ex->getMessage() . "<br/>\n";
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment