* License: MIT
*
* Very basic gallery that I coded for my own use, obviously very specific to
* my needs, but maybe parts of it may be of some interest to someone else.
*
* This code uses quite a few conventions:
* - images are in the same directory
* - image names have this format: "YYYYMMDDHHMM_image_name.jpg"
* - a text file containing descriptions is in the same directory
* - the descriptions file contains one description per line
* - each description line has this format: "filename : description"
*/
define('DESCRIPTIONS_FILE', 'descriptions.txt');
// Extract the timestamp part of a filename
function get_timestamp_part($filename) {
$parts = explode('_', $filename);
return $parts[0];
}
// Extract the name part of a filename
function get_name_part($filename) {
// Filenames all have the format YYYYMMDDHHMM_name.jpg
$name = substr($filename, 13);
$name = substr($name, 0, -4);
return $name;
}
// Load the description of an image, if any. Returns null if none found.
function get_description($filename) {
$descriptions = explode("\n", file_get_contents(DESCRIPTIONS_FILE));
foreach ($descriptions as $description) {
if (strpos($description, $filename) === 0) {
$fields = explode(' : ', $description);
return $fields[1];
}
}
return null;
}
// Pretty-print a timestamp
// Timestamps have the format YYYYMMDDHHMM
function pretty_print_timestamp($timestamp) {
$output = substr($timestamp, 0, 4); // Year
$output .= '-' . substr($timestamp, 4, 2); // Month
$output .= '-' . substr($timestamp, 6, 2); // Day
$output .= ' ' . substr($timestamp, 8, 2); // Hour
$output .= ':' . substr($timestamp, 10, 2); // Minute
return $output;
}
// Pretty-print an image name
function pretty_print_name($name) {
$pretty_name = ucfirst(str_replace('_', ' ', $name));
return $pretty_name;
}
// List all the picture files in the pictures directory
function list_picture_files() {
$picture_files = glob('*.jpg');
return $picture_files;
}
// Check a filename existence
function image_exists($filename) {
$picture_files = list_picture_files();
return (array_search($filename, $picture_files) !== false);
}
// Create a link to a picture using its filename
// The showTimestamp parameter is optional, if provided it's used to hide the timestamp
// if omitted the timestamp is automatically added.
function link_to_picture($filename, $showTimestamp = true) {
if (!isset($text)) {
if ($showTimestamp) {
$text = pretty_print_timestamp(get_timestamp_part($filename)) . ' - ' . pretty_print_name(get_name_part($filename));
} else {
$text = pretty_print_name(get_name_part($filename));
}
}
$link = '';
$link .= $text;
$link .= '';
return $link;
}
// Format a list as a HTML unordered list
function display_unordered_list($list) {
$ul = "
\n\t- " . implode("
\n\t- ", $list) . "
\n
\n";
return $ul;
}
// Display the image index
function display_index() {
return display_unordered_list(array_map("link_to_picture", list_picture_files()));
}
// Display a specific image
function display_image($filename) {
// I need to know which picture this is, for navigation links
$picture_files = list_picture_files();
$current_picture_index = array_search($filename, $picture_files);
// Actual image
$img_tag = '';
// Overlay navigation
$overlay_navigation_previous = '';
$overlay_navigation_next = '';
if ($current_picture_index !== count($picture_files) - 1) {
$overlay_navigation_next .= link_to_picture($picture_files[$current_picture_index + 1]);
}
if ($current_picture_index !== 0) {
$overlay_navigation_previous .= link_to_picture($picture_files[$current_picture_index - 1]);
}
$image_display = "\n\t" . $overlay_navigation_previous . "\n" . $img_tag . "\n" . $overlay_navigation_next . "
\n";
// Image title
$title = pretty_print_name(get_name_part($filename));
$image_display .= "" . $title . "
\n";
// Image date
$date = pretty_print_timestamp(get_timestamp_part($filename));
$image_display .= "" . $date . "
\n";
// Image description
$description = get_description($filename);
if (!is_null($description)) {
$image_display .= "" . $description . "
\n";
}
// links to previous, index and next image
$links = "\n\t";
// Link to previous image
if ($current_picture_index !== 0) {
$link_to_previous = link_to_picture($picture_files[$current_picture_index - 1], false);
$links .= "
" . $link_to_previous . "\n\t";
} else {
$links .= "
";
}
// Link to the image index
$link_to_index = '
Images index';
$links .= "
" . $link_to_index . "\n";
// Links to next image
if ($current_picture_index !== count($picture_files) - 1) {
$link_to_next = link_to_picture($picture_files[$current_picture_index + 1], false);
$links .= "
" . $link_to_next . "\n\t";
} else {
$links .= "
";
}
$links .= "
\n";
$image_display .= $links;
return $image_display;
}
// Display the latest image
function display_latest() {
$picture_files = list_picture_files();
$latest = $picture_files[count($picture_files) - 1];
return display_image($latest);
}
// Let's be fancy and call this part "routing" (at least it sounds better than "do stuff").
// If the "p" parameter is set it means we have to display a specific image.
// A special "index" value for "p" displays a list of all images.
// No "p" parameter or an empty "p" parameter displays the latest image.
function route_request($request) {
if (!empty($request['p'])) {
if ($request['p'] === 'index') {
// Display a full list of images
return display_index();
}
else {
// Display the requested image if it exists, the latest otherwise
if (image_exists($request['p'] . '.jpg')) {
return display_image($request['p'] . '.jpg');
}
else {
return display_latest();
}
}
}
else {
// Display the latest image
return display_latest();
}
}
?>
CMDR JKB's sights from Elite: Dangerous