Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created March 7, 2012 19:12
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 walterdavis/1995214 to your computer and use it in GitHub Desktop.
Save walterdavis/1995214 to your computer and use it in GitHub Desktop.
List of photos in a folder
new Ajax.Updater('list_area','comics/list.php',{method: 'get'});
document.observe('click', function(evt){
var elm;
if(elm = evt.findElement('a.ajax')){
evt.stop();
var link = elm.href;
$('photo_goes_here').update('<img src="' + link + '" alt="" />');
}
});
<?php
//CONFIGURATION:
$limit = 20; //only 20 photos
date_default_timezone_set ( 'America/New_York' );
/**
* Place this file in a folder full of images to publish a list of the photos.
*
* - Files are sorted latest-first by modification date.
*
* - If present, IPTC comments will be used for the captions. If not,
* file names are used, (underscore is replaced with a space, extension
* is removed).
*
* - JPEG, GIF, and PNG images are supported
*
* - Requires PHP5
*
* LICENSE (BSD License)
*
* Copyright (c) 2012, Walter Lee Davis <waltd@wdstudio.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
//FUNCTIONS
/**
* recursive variable replacement
*
* @param string $strText Input text including {{variable}} placeholders
* @return string with all templates replaced with existing global vars
* @author Walter Lee Davis
*/
function replace_vars($strText){
$out = preg_replace_callback('/\{\{(.+?)\}\}/im','replace_vars_callback',$strText);
if(strpos($out,'{{') !== false) {
return replace_vars($out);
}
return $out;
}
function replace_vars_callback($arrMatches){
$match = $arrMatches[1];
if(isset($GLOBALS[$match])) return $GLOBALS[$match];
//the spaces between the brackets here are Unicode Word Joiner characters (U+2060)
//not actual spaces. This keeps away the infinite loop.
return '{⁠{' . $match . '}⁠}';
}
/**
* test for variable present and not empty
*
* @param string $var Variable name
* @return void
* @author Walter Lee Davis
*/
function present($var){
return (isset($var) && !empty($var));
}
/**
* IPTC caption reader
*
* @param string $path path to file
* @return string
* @author Tim Plumb
*/
function readCaption($path){
$IPTC_Caption = "";
$size = getimagesize( $path, $info );
if (isset($info["APP13"])) {
if($iptc = iptcparse( $info["APP13"] ) ) {
$IPTC_Caption = present($iptc["2#005"][0]) ? $iptc["2#005"][0] : $iptc["2#120"][0];
}
}
return( $IPTC_Caption );
}
/**
* file extension from filename
*
* @param string $path
* @return string
* @author Walter Lee Davis
*/
function extension($path){
return strtolower(array_pop(explode('.',$path)));
}
/**
* read the current request and return the full external URL
*
* @return string
* @author Walter Lee Davis
*/
function absolute_url() {
$out = 'http' . (($_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
$out .= $_SERVER['SERVER_NAME'] . (($_SERVER['SERVER_PORT'] != '80') ? $_SERVER['SERVER_PORT'] : '');
return $out . $_SERVER['PHP_SELF'];
}
function h($key){
return htmlentities($key,ENT_COMPAT,'UTF-8');
}
//CONSTANTS
$url = dirname(absolute_url()) . '/';
$mimes = array('jpeg' => 'photo/jpeg', 'jpg' => 'photo/jpeg', 'png' => 'photo/png', 'gif' => 'photo/gif');
$dir = dirname(__FILE__);
$me = basename(__FILE__);
$files = $photos = $times = $entries = $figures = array();
$latest = $i = 0;
//MAIN LOOP
//read the directory
foreach(scandir($dir) as $file){
if(preg_match('/^[^\.].+?\.(jpe?g|png|gif)$/i', $file)){
$timestamp = filemtime($dir . '/' . $file);
$latest = ($timestamp > $latest) ? $timestamp : $latest;
$files[] = $file;
$times[] = $timestamp;
}
}
$page_updated = date('F j, Y, g:i a', $latest);
$pubdate = date('Y-m-d H:i\Z', $latest);
$latest = gmdate('Y-m-d\TH:i:s\Z',$latest);
//sort by date descending
arsort($times);
foreach($times as $k => $v){
$photos[] = array($files[$k], $times[$k]);
}
$fragment = '<ul>
{{figures}}
</ul>
';
$figure = ' <li>
<a href="{{link}}" class="ajax">
{{figure_title}} — Posted <time datetime="{{datetime}}">{{posted}}</time>
</a>
</li>
';
//build the current set of entries as an array of strings
foreach($photos as $photo){
if($i < $limit){
$datetime = date('Y-m-d H:i\Z', $photo[1]);
$posted = date('F j, Y, g:i a', $photo[1]);
$link = $url . $photo[0];
$item_title = readCaption($dir . '/' . $photo[0]);
if ($item_title === ""){
$item_title = preg_replace('/_/', ' ', preg_replace('/\.[a-zA-Z]{3,4}$/','',$photo[0]));
}
$figure_title = $item_title;
$figures[] = replace_vars($figure);
$i++;
}else{
break;
}
}
//concatenate into a single string
$figures = implode("\n",$figures);
//build the outer shell
$fragment = replace_vars($fragment);
//send the result
header('Content-type: text/html; charset=utf-8');
print $fragment;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment