Created
March 12, 2012 15:32
-
-
Save walterdavis/2022679 to your computer and use it in GitHub Desktop.
This file contains 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 | |
//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 boolean | |
* @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__); | |
$photos = $figures = array(); | |
$i = 0; | |
//MAIN LOOP | |
//read the directory | |
foreach(scandir($dir) as $file){ | |
if(preg_match('/^[^\.].+?\.(jpe?g|png|gif)$/i', $file)){ | |
$photos[] = $file; | |
} | |
} | |
//sort by date descending | |
natcasesort($photos); | |
$fragment = '<ul> | |
{{figures}} | |
</ul> | |
'; | |
$figure = ' <li> | |
<a href="{{link}}" class="ajax"> | |
{{figure_title}} | |
</a> | |
</li> | |
'; | |
//build the current set of entries as an array of strings | |
foreach($photos as $photo){ | |
if($i < $limit){ | |
$link = $url . $photo; | |
$item_title = readCaption($dir . '/' . $photo); | |
if ($item_title === ""){ | |
$item_title = preg_replace('/_/', ' ', preg_replace('/\.[a-zA-Z]{3,4}$/','',$photo)); | |
} | |
$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