Last active
March 10, 2016 04:12
-
-
Save michvaldes001/0373066e21cd7dd17e5c to your computer and use it in GitHub Desktop.
PHP based self updating photo gallery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#This script requires a "thumbnail" and "description" folder to be present in the directory where images are stored. | |
#Photos must be uploaded manually and caption files must be edited manually. | |
#Demo can be found here: http://michvaldes001.ddns.net/index.php?section_id=Gallery | |
#Main photo displaying/image processing function. | |
function photo_display($photo_directory) { | |
#Open directory of photo gallery. | |
if ($diropen = opendir($photo_directory)){ | |
#Sort though photos and text caption files. | |
while (($files = readdir($diropen)) !== false) | |
{ | |
$photo_path = ($photo_directory . "/" . $files); | |
$text_file = substr($files, 0, -3) . "txt"; | |
#Create thumbnails if thumbnails not present. Directory is "thumbnails". | |
if (!file_exists($photo_directory . "/thumbnail/" . $files)){ | |
echo "<h1>Thumbnails updating...</h2>"; | |
$photo_source_image = imagecreatefromjpeg($photo_path); | |
$width = imagesx($photo_source_image); | |
$height = imagesy($photo_source_image); | |
$photo_virtual_image = imagecreatetruecolor(300, 200); | |
imagecopyresampled($photo_virtual_image, $photo_source_image, 0, 0, 0, 0, 300, 200, $width, $height); | |
imagejpeg($photo_virtual_image, ($photo_directory. "/thumbnail/" . $files)); | |
unset($photo_source_image, $photo_virtual_image); | |
flush(); | |
echo "<meta http-equiv='refresh' content='1'>"; | |
} | |
#If create an empty text file for caption infomation if one doesn't exist. Directory is "descriptions". | |
if (!file_exists($photo_directory . "/descriptions/" . $text_file)){ | |
$text_file = ($photo_directory . "/descriptions/" . $text_file); | |
fopen($text_file, 'w'); | |
flush(); | |
} | |
#Display thumbnails (only if associated image exists). Caption will be present in alt tag but variable can be used | |
#if you intend to integrate this gallery with Lightbox or something similar. | |
if ($files !== "thumbnail" && $files !== ".." && $files !== "." && $files !== "descriptions"){ | |
$description_text = file_get_contents($photo_directory . "/descriptions/" . $text_file); | |
echo "<a href = '" . $photo_path . "'><img src = '" . $photo_directory . "/thumbnail/" . $files . "' alt = '" . $description_text ."'></a>"; | |
} | |
} | |
} | |
closedir($photo_directory); | |
} | |
#Clean up thumbnails with no associated image (optional). | |
function thumbnail_cleanup($photo_directory) { | |
if ($diropen = opendir($photo_directory . "/thumbnails")){ | |
while (($files = readdir($diropen)) !== false) | |
{ | |
$photo_path = ($photo_directory . "/thumbnails/" . $files); | |
if (!file_exists($photo_directory . "/" . $files) && ($files !== "thumbnail" || $files !== "." || $files !== ".." || $files !== "descriptions")){ | |
unlink($photo_path); | |
} | |
} | |
} | |
$closedir($photo_directory); | |
} | |
#Call the main image processing function | |
photo_display('<DIRECTORY OF YOUR GALLERY>'); | |
#Call the thumbnail cleanup fuction (optional) | |
thumbnail_cleanup('<DIRECTORY OF YOUR GALLERY'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment