@vern
Here's a PHP script you can use to display images from a designated folder on your web server which have certain specified tags.
Images should be tagged using the IPTC Keywords field (with each tag on a new line) in an imaging program such as Adobe Photoshop.
Within the script you can enter a comma-separated list of tags to match and set the tag mode to 'any' (images with any matching tags will be displayed) or 'all' (images with all matching tags will be displayed).
There are other variables you can use to order and sort the images and others to control image titles and captions.
To see this in action:
(1) Create a gallery with JuiceboxBuilder-Pro (using just a sample image to allow the gallery to be built) customizing the layout as necessary.
(2) Add the following line to your gallery's embedding code (in the 'index.html' file).
(3) Create a new file in a plain text editor with the code below (configuring the variables as required), save the file with the filename 'config.php' and place the file in your gallery folder.
(4) Leave the 'config.xml' file in your gallery folder (the PHP script will use the configuration options from there).
(5) Add the images you want to use in your gallery (tagged via IPTC Keywords) to the 'images' folder.
I hope this helps or at least points you in the right direction.
<?php
header('Content-type: application/xml');
function get_values($filename) {
$values = array();
if (is_file($filename)) {
$dom_doc = new DOMDocument('1.0', 'UTF-8');
$dom_doc->load($filename);
$settings_tags = $dom_doc->getElementsByTagName('juiceboxgallery');
$settings_tag = $settings_tags->item(0);
if (!is_null($settings_tag)) {
foreach ($settings_tag->attributes as $attribute) {
$name = $attribute->nodeName;
$value = $attribute->nodeValue;
$values[$name] = $value;
}
}
}
return $values;
}
function filter_element($value) {
return $value !== '.' && $value !== '..';
}
function sort_images_datetime($a, $b) {
$a_datetime = get_datetime($a);
$b_datetime = get_datetime($b);
if ($a_datetime === $b_datetime) {
return 0;
}
return $a_datetime < $b_datetime ? -1 : 1;
}
function clean_url($url, $directory) {
$output = rtrim(trim($url), '/');
$output = $directory ? $output . '/' : $output;
if (preg_match('/^https?:/i', $output)) {
$output = preg_replace('/^(https?:)\/*(.*)/i', '\\1//\\2', $output);
$output = strtolower(parse_url($output, PHP_URL_SCHEME)) . '://' . parse_url($output, PHP_URL_HOST) . preg_replace('/\/+/', '/', parse_url($output, PHP_URL_PATH));
} else {
$output = preg_replace('/\/+/', '/', $output);
}
return $output;
}
function get_attachments_folder($folder_url) {
$attachments = array();
if (preg_match('/^https?:\/\//', $folder_url)) {
$dom_doc = new DOMDocument('1.0', 'UTF-8');
$success = @$dom_doc->loadHTMLFile($folder_url);
if ($success) {
foreach ($dom_doc->getElementsByTagName('a') as $element) {
$attachments[] = $folder_url . rawurldecode(basename($element->getAttribute('href')));
}
}
} else {
$leading_slash = strpos($folder_url, '/') === 0;
$document_root = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
$root_dir = rtrim($document_root, '/');
$directory = $leading_slash ? $root_dir . $folder_url : realpath(dirname(__file__) . '/' . $folder_url);
if (is_dir($directory)) {
$array = @scandir($directory);
if ($array) {
$files = array_filter($array, 'filter_element');
$https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : '';
$scheme = $https === 'on' ? 'https://' : 'http://';
$path = $leading_slash ? $folder_url : str_replace($root_dir, '', $directory) . '/';
$server_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
foreach ($files as $file) {
$attachments[] = $scheme . $server_name . $path . $file;
}
}
}
}
return $attachments;
}
function process_tag_folder($attachments, $comma_separated_values, $mode) {
$output = $attachments;
$media_tags = array_filter(array_map('trim', explode(',', $comma_separated_values)), 'strlen');
$media_tags_count = count($media_tags);
if ($media_tags_count > 0) {
foreach ($attachments as $key=>$attachment) {
$image_url = encode_url($attachment);
$info = array();
$image_size = @getimagesize($image_url, $info);
$iptc = array();
if ($image_size && isset($info['APP13'])) {
$iptc = @iptcparse($info['APP13']);
}
$image_keywords = $iptc && isset($iptc['2#025']) ? $iptc['2#025'] : array();
$tags = array_filter(array_map('trim', $image_keywords), 'strlen');
$array_intersect_count = count(array_intersect($media_tags, $tags));
if (($mode === 'any' && $array_intersect_count === 0) || ($mode === 'all' && $array_intersect_count !== $media_tags_count)) {
unset($output[$key]);
}
}
}
return array_values($output);
}
function order_attachments($attachments, $order) {
$output = array();
$all = $attachments;
switch ($order) {
case 'descending':
$output = array_reverse($all);
break;
case 'shuffle':
shuffle($all);
$output = $all;
break;
case 'ascending':
default:
$output = $all;
break;
}
return $output;
}
function encode_url($url) {
$parse_url = parse_url($url);
$scheme = isset($parse_url['scheme']) ? $parse_url['scheme'] : '';
$host = isset($parse_url['host']) ? $parse_url['host'] : '';
$path = isset($parse_url['path']) ? $parse_url['path'] : '';
$query = isset($parse_url['query']) ? $parse_url['query'] : '';
$implode_path = array();
$explode_path = explode('/', $path);
foreach ($explode_path as $fragment_path) {
if ($fragment_path !== '') {
$implode_path[] = rawurlencode($fragment_path);
}
}
$new_path = !empty($implode_path) ? implode('/', $implode_path) : '';
$implode_query = array();
$explode_query = explode('&', $query);
foreach ($explode_query as $fragment_query) {
if ($fragment_query !== '') {
$explode_fragment_query = explode('=', $fragment_query, 2);
if (count($explode_fragment_query) === 2) {
$implode_query[] = $explode_fragment_query[0] . '=' . urlencode($explode_fragment_query[1]);
}
}
}
$new_query = !empty($implode_query) ? '?' . implode('&', $implode_query) : '';
return $scheme . '://' . $host . '/' . $new_path . $new_query;
}
function line_break($input) {
return preg_replace('/\r\n|\r|\n/', '<br />', $input);
}
function strip_control_characters($input) {
$output = @preg_replace('/\p{Cc}+/u', '', $input);
return $output ? $output : $input;
}
$dom_doc = new DOMDocument('1.0', 'UTF-8');
$dom_doc->formatOutput = true;
$settings_tag = $dom_doc->createElement('juiceboxgallery');
$gallery_filename = 'config.xml';
if (is_file($gallery_filename)) {
$values = get_values($gallery_filename);
foreach ($values as $key=>$value) {
$settings_tag->setAttribute($key, $value);
}
}
$custom_values = array();
$custom_values['e_folderUrl'] = 'images'; // Path to folder containing images
$custom_values['e_folderTags'] = 'one,two'; // Comma-separated list of tags
$custom_values['e_folderTagMode'] = 'any'; // Can be 'all' or 'any'
$custom_values['e_folderTitle'] = 'filename'; // Can be 'filename' or 'iptc'
$custom_values['e_folderCaption'] = 'iptc'; // Can be 'filename' or 'iptc'
$custom_values['e_folderSort'] = 'filename'; // Can be 'filename' or 'date'
$custom_values['e_folderOrder'] = 'ascending'; // Can be 'ascending', 'descending' or 'shuffle'
$custom_values['e_linkTarget'] = '_blank'; // Can be '_blank', '_parent', '_self' or '_top'
$custom_values['e_displayTitle'] = 'true'; // Can br 'true' or 'false'
$custom_values['e_displayCaption'] = 'true'; // Can br 'true' or 'false'
$custom_values['e_folderUrl'] = clean_url($custom_values['e_folderUrl'], true);
$custom_values['e_folderTags'] = trim($custom_values['e_folderTags']);
$custom_values['e_displayTitle'] = isset($custom_values['e_displayTitle']) ? $custom_values['e_displayTitle'] : 'false';
$custom_values['e_displayCaption'] = isset($custom_values['e_displayCaption']) ? $custom_values['e_displayCaption'] : 'false';
$attachments = get_attachments_folder($custom_values['e_folderUrl']);
if ($attachments) {
$attachments = process_tag_folder($attachments, $custom_values['e_folderTags'], $custom_values['e_folderTagMode']);
switch ($custom_values['e_folderSort']) {
case 'filename':
natcasesort($attachments);
break;
case 'date':
usort($attachments, 'sort_images_datetime');
break;
default:
break;
}
$attachments = order_attachments($attachments, $custom_values['e_folderOrder']);
foreach ($attachments as $attachment) {
$image_url = encode_url($attachment);
$iptc = array();
if (($custom_values['e_displayTitle'] === 'true' && $custom_values['e_folderTitle'] === 'iptc') || ($custom_values['e_displayCaption'] === 'true' && $custom_values['e_folderCaption'] === 'iptc')) {
$info = array();
$image_size = @getimagesize($image_url, $info);
if ($image_size && isset($info['APP13'])) {
$iptc = @iptcparse($info['APP13']);
}
}
$image_element = $dom_doc->createElement('image');
$image_element->setAttribute('imageURL', $image_url);
$image_element->setAttribute('thumbURL', $image_url);
$image_element->setAttribute('linkURL', $image_url);
$image_element->setAttribute('linkTarget', $custom_values['e_linkTarget']);
if ($custom_values['e_displayTitle'] === 'true') {
$title_element = $dom_doc->createElement('title');
$image_title = '';
switch ($custom_values['e_folderTitle']) {
case 'filename':
$image_title = pathinfo($attachment, PATHINFO_FILENAME);
break;
case 'iptc':
$image_title = $iptc && isset($iptc['2#005']) ? $iptc['2#005'][0] : '';
break;
default:
break;
}
$image_title = line_break($image_title);
$image_title = strip_control_characters($image_title);
$title_text = $dom_doc->createCDATASection($image_title);
$title_element->appendChild($title_text);
$image_element->appendChild($title_element);
}
if ($custom_values['e_displayCaption'] === 'true') {
$caption_element = $dom_doc->createElement('caption');
$image_caption = '';
switch ($custom_values['e_folderCaption']) {
case 'filename':
$image_caption = pathinfo($attachment, PATHINFO_FILENAME);
break;
case 'iptc':
$image_caption = $iptc && isset($iptc['2#120']) ? $iptc['2#120'][0] : '';
break;
default:
break;
}
$image_caption = line_break($image_caption);
$image_caption = strip_control_characters($image_caption);
$caption_text = $dom_doc->createCDATASection($image_caption);
$caption_element->appendChild($caption_text);
$image_element->appendChild($caption_element);
}
$settings_tag->appendChild($image_element);
}
}
$dom_doc->appendChild($settings_tag);
echo $dom_doc->saveXML();
?>