getHeader(); // wrap options in an object so they can be passed around easily $options = new Options($svOptions, $bgOptions); print '

( buildgallery.php version 1.9, running under php version '.phpversion().', GD library version '.$options->gdVersion.')

'; // collect data about image files in the $imageFiles object $imageFiles = new ImageFiles($options); $fileNames = $imageFiles->getFileNames(); // attempt to create xml file $galleryXml = new GalleryXml($fileNames, $options); // attempt to create thumbnails $thumbnails = new Thumbnails($fileNames, $options); // close html tags print '

buildgallery script complete

'; print $page->getFooter(); // ----------------------------------------------------------------------------------------------- // Class Options // contains options for simpleviewer and buildgallery class Options { // array svOptions simpleviewer options var $svOptions; // array bgOptions buildgallery options var $bgOptions; // string gdVersion version number of GD library var $gdVersion; // floating point number gdVersionNumber version number of GD library expressed as a number // contents of $gdVersion after the second decimal point will be lost // function Options constructs Options pre-processes path data and gets GD version function Options($svOptions, $bgOptions) { $this->svOptions = $svOptions; $this->bgOptions = $bgOptions; $this->bgOptions['imagePath'] = $this->getPath('imagePath', $this->svOptions['imagePath']); $this->bgOptions['thumbPath'] = $this->getPath('thumbPath', $this->svOptions['thumbPath']); $this->gdVersion = $this->getGDVersion(); $this->gdVersionNumber = (float)$this->gdVersion; } // function getPath extracts image or thumb path from simpleviewer options function getPath($pathName, $path) { // handle default path if ($path == '' && $pathName == 'imagePath') {return 'images';} if ($path == '' && $pathName == 'thumbPath') {return 'thumbs';} // handle correctly formatted path with trailing / $lastChar = substr($path, -1); if ($lastChar == '/') { return substr($path, 0, -1); } return $path; } // function getGDVersion // returns string $gd_version_number // Use output buffering to get results from phpinfo() // without disturbing the page we're in. Output // buffering is "stackable" so we don't even have to // worry about previous or encompassing buffering. function getGDversion() { static $gd_version_number = null; if ($gd_version_number === null) { ob_start(); phpinfo(8); $module_info = ob_get_contents(); ob_end_clean(); if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches)) { $gd_version_number = $matches[1]; } else { $gd_version_number = 0; } } return $gd_version_number; } } // Class ImageFiles // Extracts information about the files in the designated image directory class ImageFiles { // array $fileNames names and dates of files in images directory var $fileNames; // function ImageFiles constructs ImageFiles // parameter object $options simpleviewer and buildgallery options function ImageFiles($options) { $this->fileNames = array(); $imageDir = $options->bgOptions['imagePath']; if (!is_dir($imageDir)) { die('

Error: the image directory '.$imageDir.' cannot be found

'); } $folder = @opendir($imageDir); if ($folder === false) { die('

Cannot open the '.$imageDir.' directory – check the file permissions.

'); } $jpgCount = 0; $sortMessage = ''; while(false !== ($fileName = readdir($folder))) { if (!$this->isImage($fileName)) {continue;} if ($options->bgOptions['sortImagesByDate']) { $this->fileNames[$fileName] = filemtime($imageDir.'/'.$fileName); $sortMessage = 'Sorting images by date. '; } else { $this->fileNames[$fileName] = $fileName; $sortMessage = 'Sorting images by file name. '; } $jpgCount ++; } if ($jpgCount == 0) { print '

Warning: no jpg images found in '.$imageDir.' folder.

'; return; } else { print '

'.$jpgCount.' jpg images found in '.$imageDir.' folder.

'; } // now sort by date modified if ($options->bgOptions['sortInReverseOrder']) { $sortMessage .= 'Sort in reverse order.'; arsort($this->fileNames); } else { $sortMessage .= 'Sort in forward order.'; asort($this->fileNames); } closedir($folder); print '

'.$sortMessage.'

'; print '
    '; foreach ($this->fileNames as $fileName => $value) { print '
  1. '.$fileName.'
  2. '; } print '
'; } // function getFileNames discards date information and returns array of image file names function getFileNames() { return array_keys($this->fileNames); } // function isImage returns true if $fileName has a suffix .jpg or .jpeg, otherwise returns false // parameter string file name function isImage($fileName) { if ($fileName[0] == "." || $fileName[0] == ".." ) {return false;} $components = explode(".", $fileName); $length = count($components); if ($length == 1) {return false;} $suffix = strtolower($components[$length - 1]); if ($suffix == 'jpg' || $suffix == 'jpeg') {return true;} return false; } } // class GalleryXml creates gallery.xml file class GalleryXml { // var string $xml contents of gallery.xml file var $xml; // function GalleryXml constructs XmlDoc // parameter array $fileNames names of image files // parameter object $options user options function GalleryXml($fileNames, $options) { $this->xml = '\n"; $this->xml .= $this->getXmlOptions($options)."\n"; foreach ($fileNames as $fileName) { $this->xml .= "\n"; $this->xml .= "\t".$fileName."\n"; //add auto captions if ($options->bgOptions['showDownloadLinks']) { $this->xml .= "\t".'bgOptions['imagePath'].'/'.$fileName.'" target="_blank">Open image in new window]]>'."\n"; } $this->xml .= "\n"; } $this->xml .= ''; $file = "gallery.xml"; // attempt to change permissions if (file_exists($file)) { @chmod($file, 0777); } if (!$file_handle = @fopen($file,"w")) { print '

Cannot open XML document: $file. Change permissions to 0777 for $file and parent directory.

'; } elseif (!@fwrite($file_handle, $this->xml)) { print '

Cannot write to XML document: $file. Change permissions to 0777 for file and parent directory.

'; } else { print '

Successfully created XML document: '.$file.'

'; } @fclose($file_handle); // attempt to change file permissions for later editing by user @chmod($file, 0777); } // function getXmlOptions formats simpleviewer options as an xml tag function getXmlOptions($options) { $xmlOptions = 'svOptions as $optName => $optValue) { $xmlOptions = $xmlOptions .= ' '.$optName.' = "'.$optValue.'"'; } $xmlOptions = $xmlOptions .= '>'; return $xmlOptions; } } class Thumbnails { // function Thumbnails constructs Thumbnails class // parameter array $fileNames names of source images // parameter object $options options for simpleviewer and buildgallery function Thumbnails($fileNames, $options) { $imageDir = $options->bgOptions['imagePath']; $thumbDir = $options->bgOptions['thumbPath']; if (!is_dir($thumbDir)) { // Note: mkdir($thumbDir, 0777) will not work reliably because of php's umask (www.php.net/umask) mkdir($thumbDir); chmod($thumbDir, 0777); } if ($options->gdVersionNumber < 1.8) { print '

Warning: the GD imaging library was not found on this server or it is an old version that does not support jpeg images. Thumbnails will not be created. Either upgrade to a later version of GD or create the thumbnails yourself in a graphics application such as Photoshop.

'; return; } elseif ($options->gdVersionNumber < 2) { print '

Warning: the GD imaging library on this server is version '.$options->gdVersion.'. Thumbnails will be of lower quality. You might want to upgrade to a later version of GD or create the thumbnails yourself in a graphics application such as Photoshop.

'; } elseif ($options->bgOptions['useCopyResized']) { print '

Warning: the useCopyResized setting has been set to true. Thumbnails will be of lower quality. You might want to create the thumbnails yourself in a graphics application such as Photoshop.

'; } print '

Attempting to create thumbnails in '.$thumbDir.' folder:

'; $thumbCount = 0; $thumbList = '
    '; foreach ($fileNames as $fileName) { $filePath = $imageDir.'/'.$fileName; $thumbPath = $thumbDir.'/'.$fileName; $thumbCount ++; if (file_exists($thumbPath) && !$options->bgOptions['overwriteThumbnails']) { $thumbList .= '
  1. '.$fileName.' already exists
  2. '; continue; } if ($this->createThumb($filePath, $thumbPath, $options)) { $thumbList .= '
  3. '.$fileName.' created
  4. '; } else { $thumbList .= '
  5. '.$fileName.' could not be created
  6. '; } } $thumbList .= '
'; if ($thumbCount > 0) {print $thumbList;} } // function createThumb creates and saves thumbnail image. // returns boolean $success // uses older GD library functions if current ones are not available // parameter string $filePath path to source image // parameter string $thumbPath path to new thumbnail // parameter object $options options function createThumb($filePath, $thumbPath, $options) { $thumbSize = 65; $quality = 85; // Get the image dimensions. $dimensions = @getimagesize($filePath); $width = $dimensions[0]; $height = $dimensions[1]; $smallerSide = min($width, $height); // Calculate offset of square portion of image // offsets will both be zero if original image is square $deltaX = ($width - $smallerSide)/2; $deltaY = ($height - $smallerSide)/2; // get image identifier for source image $imageSrc = @imagecreatefromjpeg($filePath); // Create an empty thumbnail image. if ($options->gdVersionNumber < 2 || $options->bgOptions['useCopyResized']) { $imageDest = @imagecreate($thumbSize, $thumbSize); $success = @imagecopyresized($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $thumbSize, $thumbSize, $smallerSide, $smallerSide); } else { $imageDest = @imagecreatetruecolor($thumbSize, $thumbSize); $success = @imagecopyresampled($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $thumbSize, $thumbSize, $smallerSide, $smallerSide); } if (!$success) {return false;} { // save the thumbnail image into a file. $success = @imagejpeg($imageDest, $thumbPath, $quality); // Delete both image resources. @imagedestroy($imageSrc); @imagedestroy($imageDest); } return $success; } } // Class Page produces html headers and footers class Page { // string $title contents for html tags var $title; // function Page constructs Page class function Page($title) { $this->title = $title; } // Function getHeader // returns string containing html header, css styles and page heading // rules for heredoc ( << $this->title

Creating XML and thumbnails for SimpleViewer.

EOD; return $header; } // function getFooter returns string containing closing html tags // could also be used for a page footer, such as a copyright message // rules for heredoc ( << EOD; return $footer; } } ?>