PDA

View Full Version : PHP Image Gallery



Yeldarb
July 1st, 2005, 03:43 PM
I wrote this this morning to dynamically generate thumbnails and display the thumbnails with links to the full sized pictures because I was tired of having to download the entire picture to see my digital photos (they're all online; previously they were in the apache default directory viewer).

Put this in a php file in the folder and make sure the folder is chmodded to 0777 so that the script can write to the thumbnail directory that it will create.

Once the page has been accessed once (and the thumbnails are created), it will just use the previously generated files and not use up all that CPU power converting the pictures again.

Requires PHP GD library.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thumbnails</title>
<style type="text/css">
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
}
</style>
</head>

<body>
<i><?PHP echo stripslashes($_SERVER['PHP_SELF']); ?></i><br /><br />
<?PHP
$directory = ".";

if(!file_exists("./thumbs")) {
//If there is no "thumbs" directory, create a thumbnail directory
mkdir("./thumbs");
}

$myDirectory = opendir($directory);
while($entryName = readdir($myDirectory)) {
if(is_dir($entryName)) {
if($entryName != "." && $entryName != "thumbs") {
if($entryName == "..") {
echo "<img src=\"/icons/folder.gif\" /> <a href=\"../\">Parent Directory</a><br /><br />\n\n";
} else {
echo "<img src=\"/icons/folder.gif\" /> <a href=\"./" . $entryName . "\">" . $entryName . "</a><br />\n";
}
}
}
}
echo "<br />\n";

closedir($myDirectory);

$myDirectory = opendir($directory);
echo "<table>\n";
echo "<tr>";
$i = -1;
while($entryName = readdir($myDirectory)) {
if($entryName != "Thumbs.db" && $entryName != "thumbnails.php" && is_file($entryName)) {
if(strtoupper(substr($entryName, -4)) == ".JPG" || strtoupper(substr($entryName, -5)) == ".JPEG") {
$i++;
//If the File is a Jpeg, Check if its Thumbnail Exists
//If it does, display it with a link to the image
//If not, create one first.
if(!file_exists("./thumbs/" . $entryName)) {
thumbnail(".", "./thumbs", $entryName, 150);
}
if(!($i%4)) {
echo "</tr><tr>";
}
echo "<td>";
echo "<a href=\"" . $entryName . "\"><img src=\"./thumbs/" . $entryName . "\" /></a><br />\n";
echo "<a href=\"" . $entryName . "\">" . $entryName . "</a><br /><br />\n\n";
echo "</td>";
} else {
//echo $entryName . "<br /><br />\n\n";
}
}
}
echo "</tr>";
echo "</table>\n";
closedir($myDirectory);

function thumbnail($image_path, $thumb_path, $image_name, $thumb_width) {
$src_img = imagecreatefromjpeg("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$new_w;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_ w,$new_h,imagesx($src_img),imagesy($src_img));

imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}
?>
</body>
</html>


Edit: thumbnail function is a slightly modified one from the PHP.net handbook.

hl
July 1st, 2005, 04:18 PM
basically like a directory indexer :thumb: good job

edit:/ there's a thumbail() function? it's not defined anywhere in your code.
edit2:/ sorry, see it now at the bottom :)

eyezberg
July 1st, 2005, 04:47 PM
http://binarybonsai.com/archives/2004/08/20/callout-for-a-gallery-solution/
http://www.noscope.com/journal/2005/04/photoalbum-cms

Yeldarb
July 1st, 2005, 04:54 PM
I looked into a few photo gallery solutions eyez, but none of them really did it for me. I just needed something really simple, and with an hour or two of programming, this did the job :P

Also, most of the photo galleries that I found made you use their organization scheme and "upload" photos. I already had all of my photos there, just needed them thumbnailed :D

koolkrasher
September 27th, 2007, 04:14 PM
hahaha i been looking for osmething liek this for weeks! thanks yeldarb

Seb Hughes
September 27th, 2007, 05:38 PM
Edit: This kicks ***. Later im gonna make it so the link and image opens in light room. :D

koolkrasher
September 28th, 2007, 02:14 PM
oooo post that when its done id like to see it

Seb Hughes
September 28th, 2007, 03:56 PM
To you lightbox with this firstly get lightbox from:

http://www.huddletogether.com/projects/lightbox2/ and upload the files to the main folder thing, below is the code to use lightbox with it:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thumbnails</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
<style type="text/css">
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
}
</style>

</head>

<body>
<i><?PHP echo stripslashes($_SERVER['PHP_SELF']); ?></i><br /><br />
<?PHP

$directory = ".";

if(!file_exists("./thumbs")) {
//If there is no "thumbs" directory, create a thumbnail directory
mkdir("./thumbs");
}

$myDirectory = opendir($directory);
while($entryName = readdir($myDirectory)) {
if(is_dir($entryName)) {
if($entryName != "." && $entryName != "thumbs" && $entryName != "images" && $entryName != "css" && $entryName != "js") {
if($entryName == "..") {
echo "<img src=\"/icons/folder.gif\" /> <a href=\"../\">Parent Directory</a><br /><br />\n\n";
} else {
echo "<img src=\"/icons/folder.gif\" /> <a href=\"./" . $entryName . "\">" . $entryName . "</a><br />\n";
}
}
}
}
echo "<br />\n";

closedir($myDirectory);

$myDirectory = opendir($directory);
echo "<table>\n";
echo "<tr>";
$i = -1;

while($entryName = readdir($myDirectory)) {
if($entryName != "Thumbs.db" && $entryName != "thumbnails.php" && is_file($entryName)) {
if(strtoupper(substr($entryName, -4)) == ".JPG" || strtoupper(substr($entryName, -5)) == ".JPEG") {
$i++;
//If the File is a Jpeg, Check if its Thumbnail Exists
//If it does, display it with a link to the image
//If not, create one first.
if(!file_exists("./thumbs/" . $entryName)) {
thumbnail(".", "./thumbs", $entryName, 150);
}
if(!($i%4)) {
echo "</tr><tr>";
}
$parts = explode(".", $entryName);
$displayName = strtolower($parts[0]);

echo "<td>";
echo "<a href=\"" . $entryName . "\" rel=\"lightbox\"><img src=\"./thumbs/" . $entryName . "\" /></a><br />\n";
echo "<a href=\"" . $entryName . "\" rel=\"lightbox\">" . $displayName . "</a><br /><br />\n\n";
echo "</td>";
} else {
//echo $entryName . "<br /><br />\n\n";
}
}
}
echo "</tr>";
echo "</table>\n";
closedir($myDirectory);


function thumbnail($image_path, $thumb_path, $image_name, $thumb_width) {
$src_img = imagecreatefromjpeg("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$new_w;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_ w,$new_h,imagesx($src_img),imagesy($src_img));

imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}
?>
</body>
</html>


I also made it so when it echos the file name it doesnt show the extension.

koolkrasher
September 30th, 2007, 03:41 AM
thats hott.
Can you make it display only like 10 per page then create a ne page for another set of 10?

Seb Hughes
September 30th, 2007, 07:56 AM
thats hott.
Can you make it display only like 10 per page then create a ne page for another set of 10?

It is possible, first you need to count the number of photos then pagnate it from there.