2010-03-18, 03:38:52
I have a script that I use now and then to quickBuild image galleries. It was originally written for use with sNews but there are no sNews-specific details or hooks in the script, and it was believed that it could run in almost any (php) environment. Having tried out juliancc's Simple Image Gallery plugin, I come to realise that I would -- for my own sites -- rather use this script. For one thing it lets me create many galleries, not just one, so that's a start. And it's really simple to use. And since it's just for my own needs, I can change the output around as I see fit with relative ease. What I can't do, at the moment, is make it work with GS. So I come here asking why, and what needs to be done to make it work - if at all possible.
The script comes in two parts, one general processor that should basically be called in once in a template and then left alone; and the other part is the specific gallery handler, a copy of which is created for each new gallery needed (and of course some variables changed, like paths). I've tried looking at the details of the scripts, and I've looked at the details of Julian's Simple Gallery plugin to see how it works. I get scared just by doing this, and then the scripts (ALL of them) stand up on their skanky hind legs and shoo me. It's nasty, I tell you. Let me show you:
gallerycode.php
To make this work, I need a folder of images and, within that folder, another folder for the thumbs (which I batchCreate in PS), plus this part:
gallery.php
As I said, gallerycode.php is included through a call in the template. Then, in the GS admin, I call in gallery.php on the page I want the thumbnails to appear. Trouble is, with GS, the scripts don't work and I can't even begin to understand what it is that migh cause this non-working thing to happen. Now, if it can't be done then it can't be done. But if it's easy enough to spot why it's not working and what needs changing, then I would surely appreciate some help.
Thanks.
The script comes in two parts, one general processor that should basically be called in once in a template and then left alone; and the other part is the specific gallery handler, a copy of which is created for each new gallery needed (and of course some variables changed, like paths). I've tried looking at the details of the scripts, and I've looked at the details of Julian's Simple Gallery plugin to see how it works. I get scared just by doing this, and then the scripts (ALL of them) stand up on their skanky hind legs and shoo me. It's nasty, I tell you. Let me show you:
gallerycode.php
Code:
<?php
function get_gallery($viewPath, $dirPath, $imgClass = '', $ordered = true )
{
$dirObj = dir( $dirPath );
$i = 0;
$strOutput = "";
$arrImages = array();
while (false !== ($entryName = $dirObj->read()))
{
if (is_file( $dirPath . $entryName ))
{
$fileInfo = pathinfo( $dirPath . $entryName );
$fileExtension = $fileInfo['extension'];
$extLength = strlen($fileExtension);
$entry = substr($entryName, 0, -$extLength-1);
if ( is_image_extension( $fileInfo["extension"] ) )
{
++$i;
if ( $ordered )
{
$arrImages[$entry] = $entryName;
}
else
{
$viewLink = '<a class="thumb left" href="'.$viewPath.$entryName.'" title="'.$entry.'" rel="shadowbox['.$galleryTitle.']">';
$strOutput .= $viewLink.'<img class="'.$imgClass.'" src="'.$dirPath.$entryName.'" alt="" /></a>
';
}
}
}
} // End while
$dirObj->close();
if ( $i > 0 )
{
if ( $ordered )
{
// Sort images
asort( $arrImages );
// fill $strOutput
foreach ( $arrImages as $entry => $entryName )
{
$viewLink = '<a class="thumb left" href="'.$viewPath.$entryName.'" title="'.$entry.'" rel="shadowbox['.$galleryTitle.']">';
$strOutput .= $viewLink.'<img class="'.$imgClass.'" src="'.$dirPath.$entryName.'" alt="'.$entry.'" /></a>
';
}
}
$strOutput = $strOutput;
}
return $strOutput;
}
function show_gallery($viewPath, $dirPath, $imgClass = '', $ordered = true) {
if ( $strGallery = get_gallery($viewPath, $dirPath, $imgClass) )
echo $strGallery;
}
function is_image_extension( $strExtension = '') {
if ( ($strExtension > '') && in_array( $strExtension, array( 'gif', 'png', 'jpg', 'jpe', 'jpeg' ) ))
return true;
return false;
}
?>
To make this work, I need a folder of images and, within that folder, another folder for the thumbs (which I batchCreate in PS), plus this part:
gallery.php
Code:
<?php
$galleryDir = 'galleries/'; // Set to the directory that contains gallery images
$galleryTitle = 'Gallery Title';
$useThumbsDir = true; // Expect to find thumbs directory in $galleryDir
$mainImgClass = 'full'; // Add to css and here
$thumbImgClass = 'thm'; // Add to css and here
$sortImages = true;
// Set to the directory that contains galleries
$dirPath = '' . (($galleryDir !== '') ? ($galleryDir . '/') : '') ;
if ($useTitle && ($galleryTitle !== '')) {
echo "<h2>$galleryTitle</h2>";
}
// Process main
if ($useThumbsDir) {
$viewPath = $dirPath;
$dirPathThumbs = $dirPath . 'thumbs/';
show_gallery($viewPath, $dirPathThumbs, $thumbImgClass, $sortImages);
echo '<section class="clearer"></section>
';
}
else {
show_gallery($viewPath, $dirPath, $mainImgClass, $sortImages);
}
function get_gallery($viewPath, $dirPath, $imgClass = '', $ordered = true )
{
$dirObj = dir( $dirPath );
$i = 0;
$strOutput = "";
$arrImages = array();
while (false !== ($entryName = $dirObj->read()))
{
if (is_file( $dirPath . $entryName ))
{
$fileInfo = pathinfo( $dirPath . $entryName );
$fileExtension = $fileInfo['extension'];
$extLength = strlen($fileExtension);
$entry = substr($entryName, 0, -$extLength-1);
if ( is_image_extension( $fileInfo["extension"] ) )
{
++$i;
if ( $ordered )
{
$arrImages[$entry] = $entryName;
}
else
{
$viewLink = '<a class="thumb left" href="gallery/'.$entry.'/" title="'.$entry.'" rel="shadowbox['.$galleryTitle.']">';
$strOutput .= $viewLink.'<img class="'.$imgClass.'" src="'.$dirPath.$entryName.'" alt="" /></a></section>
';
}
}
}
} // End while
$dirObj->close();
if ( $i > 0 )
{
if ( $ordered )
{
// Sort images
asort( $arrImages );
// fill $strOutput
foreach ( $arrImages as $entry => $entryName )
{
$viewLink = '<a class="thumb left" href="gallery/'.$entry.'/" title="'.$entry.'" rel="shadowbox['.$galleryTitle.']">';
$strOutput .= $viewLink.'<img class="'.$imgClass.'" src="'.$dirPath.$entryName.'" alt="'.$entry.'" /></a>
';
}
}
$strOutput = $strOutput;
}
return $strOutput;
}
function show_gallery($viewPath, $dirPath, $imgClass = '', $ordered = true) {
if ( $strGallery = get_gallery($viewPath, $dirPath, $imgClass) )
echo $strGallery;
}
function is_image_extension( $strExtension = '') {
if ( ($strExtension > '') && in_array( $strExtension, array( 'gif', 'png', 'jpg', 'jpe', 'jpeg' ) ))
return true;
return false;
}
?>
As I said, gallerycode.php is included through a call in the template. Then, in the GS admin, I call in gallery.php on the page I want the thumbnails to appear. Trouble is, with GS, the scripts don't work and I can't even begin to understand what it is that migh cause this non-working thing to happen. Now, if it can't be done then it can't be done. But if it's easy enough to spot why it's not working and what needs changing, then I would surely appreciate some help.
Thanks.
There's nothing more foolish than a man chasing his hat.