Example: - place the following in your page instead of an img tag: - The example above would result in this replacement in the output html file: whereas if you set div mode to true with imgArraySetDivMode(true), div and span tags will be included like this (line wraps are mine):
Randomly selected image Randomly selected image
use imgArraySetStrictSizeMode(true) to get width and height attributes included in the img tag you may get a set number to imgArrayShuffle and the imgArrayTag functions in order to use more than one set of random images in the same page (useful if you have different sizes of images) if you add an imgArrayCaptions.txt file to each directory that contains images, you can specify what the alt text will be for each image if you place a '/' at the beginning of the directory name in an imgArrayShuffle call, it will search for the directory given starting at the root directory on the server, and image tags that are generated with an imgArrayTag call will use relative file names (however many '../' sets are needed) to refer to the image in the correct location. This can be handy when your images are stored in one common location, and you can include the following code in any file on your directory tree without changing it: the imgArrayTag call could produce something like this: Written by Van A. Boughner (http://hovercloud.com) - December 2003 You have permission to use and modify this file as you desire. Please give credit where credit is due, and leave my name somewhere within these comments if the code is still mainly derived from my own. Thank you! */ // default setting for div mode (when div mode is true all image tags // will be surrounded by a div tag that may also contain a caption for // the image loaded from a caption file, if there is a class name set, // it will be used for the div tag, if there is caption text for the // image, it will be included in a span tag after the image $imgArrayDivMode = false; // default setting for the strict height and width mode, if this is set // to true then the height and width specifications for images will be // followed and these attributes included in their tags $imgArrayStrictSizeMode = false; // set imgArrayCaptionMode to true if you want captions after the image tags $imgArrayCaptionMode = false; // default class name $imgArrayClassName = "imgArray"; // default border width $imgArrayBorderWidth = ""; // default alt and title tag text $imgArrayDefaultAltText = "Randomly selected image"; // default filename for caption text $imgArrayCaptionFilename = "imgArrayCaptions.txt"; // default list of image file extensions $imgArrayFileExtensionList = array("jpg","gif","png","jpeg","JPG"); function imgArraySetDivMode($flag) { global $imgArrayDivMode; $imgArrayDivMode = $flag; } function imgArraySetStrictSizeMode($flag) { global $imgArrayStrictSizeMode; $imgArrayStrictSizeMode = $flag; } function imgArraySetCaptionMode($flag) { global $imgArrayCaptionMode; $imgArrayCaptionMode = $flag; } function imgArraySetClassName($className) { global $imgArrayClassName; $imgArrayClassName = $className; } function imgArraySetBorderWidth($borderWidth) { global $imgArrayBorderWidth; $imgArrayBorderWidth = $borderWidth; } function imgArraySetCaptionFilename($filename) { global $imgArrayCaptionFilename; $imgArrayCaptionFilename = $filename; } function imgArrayCombineDirAndFile($dir, $file) { if (isset($dir) && strlen($dir) > 0) { return $dir . "/" . $file; } else { return $file; } } function imgArrayGetFileNameExtension($filename) { $fileNameParts = explode(".", $filename); $lastFieldNum = count($fileNameParts) - 1; return $fileNameParts[$lastFieldNum]; } function imgArrayRecurseIntoDirectory(&$fileList, $directoryName) { global $imgArrayFileExtensionList; $dir = opendir($directoryName); while (($filename = readdir($dir)) !== false) { if ($filename != "." && $filename != "..") { if (in_array(imgArrayGetFileNameExtension($filename), $imgArrayFileExtensionList)) { array_push($fileList, imgArrayCombineDirAndFile($directoryName, $filename)); } else if (is_dir(imgArrayCombineDirAndFile($directoryName, $filename))) { imgArrayRecurseIntoDirectory( &$fileList, imgArrayCombineDirAndFile($directoryName, $filename)); } } } closedir($dir); } function imgArrayFixAbsDirRef($directoryName) { // determine if the directory is absolute (it starts with a slash) if (strlen($directoryName) > 1 && substr($directoryName, 0, 1) == '/') { // if so, count the levels from the top this document is and // manufacture a prefix for the directory name that contains enough // '../' sets to get to the document root. The documentFilename always // contains at least one '/' and a valid token, so we skip those $prefix = ''; $documentFilename = substr($_SERVER['PHP_SELF'], 1); $token = strtok($documentFilename, '/'); $token = strtok('/'); while ($token != '') { $prefix .= '../'; $token = strtok('/'); } return $prefix . substr($directoryName, 1); } else { // otherwise, do not change the directory name (it's already relative) return $directoryName; } } function imgArrayShuffle($directoryName, $setNumber = 0) { global $imgArrayFileList; global $imgArrayFileListCount; srand((double)microtime()*1000000); if (!isset($imgArrayFileList)) { $imgArrayFileList = array(); $imgArrayFileListCount = array(); } $imgArrayFileList[$setNumber] = array(); $directoryName = imgArrayFixAbsDirRef($directoryName); imgArrayRecurseIntoDirectory(&$imgArrayFileList[$setNumber], $directoryName); shuffle($imgArrayFileList[$setNumber]); $imgArrayFileListCount[$setNumber] = 0; } function imgArrayLoadImageGetSize($filename) { $img = null; $extension = strtolower(end(explode('.', $filename))); if ($extension == 'jpg' || $extension == 'jpeg') { $img = @imagecreatefromjpeg($filename); } else if ($extension == 'png') { $img = @imagecreatefrompng($image_path); } if ($img) { $retval = array(); $retval['width'] = imagesx($img); $retval['height'] = imagesy($img); imagedestroy($img); return $retval; } else { return null; } } function imgArrayGetCaption($filename) { global $imgArrayCaptionFilename; $link = null; $retval = null; $baseImgFilename = basename($filename); $directoryName = dirname($filename); if (strlen($directoryName) > 0) { $directoryName .= "/"; } $captionFilename = $directoryName . $imgArrayCaptionFilename; @$fp = fopen($captionFilename, "r"); while (!feof($fp) && $retval == null) { $line = fgetss($fp, 2001); // fgetss strips out html tags from the input $colonPosition = strpos($line, ":"); if ($colonPosition !== false && $colonPosition > 0 && $colonPosition + 1 < strlen($line)) { $lineFilename = substr($line, 0, $colonPosition); // if there is a link, it will be after a comma, need to get it out $commaPosition = strpos($lineFilename, ","); if ($commaPosition !== false && $commaPosition > 0 && $commaPosition + 1 < strlen($lineFilename)) { $link = htmlspecialchars(trim(substr($lineFilename, $commaPosition + 1))); $lineFilename = trim(substr($lineFilename, 0, $commaPosition)); } if (!strcmp($baseImgFilename, $lineFilename)) { $retval = htmlspecialchars(trim(substr($line, $colonPosition + 1))); } } } fclose($fp); return $retval; } function imgArrayTag($setNumber = 0, $width = "150", $height = "200") { global $imgArrayDivMode; global $imgArrayFileList; global $imgArrayFileListCount; global $imgArrayClassName; global $imgArrayBorderWidth; global $imgArrayDefaultAltText; global $imgArrayStrictSizeMode; global $imgArrayCaptionMode; if ($imgArrayFileListCount[$setNumber] >= sizeof($imgArrayFileList[$setNumber])) { $imgArrayFileListCount[$setNumber] = 0; } $filename = $imgArrayFileList[$setNumber][$imgArrayFileListCount[$setNumber]]; $imgArrayFileListCount[$setNumber]++; if (isset($imgArrayClassName) && strlen($imgArrayClassName) > 0) { $classAttribute = sprintf(" class=\"%s\"", $imgArrayClassName); } else { $classAttribute = ""; } if ($imgArrayStrictSizeMode) { $sizeAttributes = sprintf(" width=\"%s\" height=\"%s\"", $width, $height); } else { $sizeArray = imgArrayLoadImageGetSize($filename); if ($sizeArray != null) { $sizeAttributes = sprintf(" width=\"%s\" height=\"%s\"", $sizeArray['width'], $sizeArray['height']); } else { $sizeAttributes = ""; } } $caption = imgArrayGetCaption($filename); if ($caption == null || strlen($caption) == 0) { $caption = $imgArrayDefaultAltText; } if (isset($imgArrayDefaultAltText) && strlen($imgArrayDefaultAltText) > 0) { $altAttribute = sprintf(" alt=\"%s\"", $caption); $titleAttribute = sprintf(" title=\"%s\"", $caption); } else { $altAttribute = ""; $titleAttribute = ""; } if (isset($imgArrayBorderWidth) && strlen($imgArrayBorderWidth) > 0) { $borderAttribute = sprintf(" border=\"%s\"", $imgArrayBorderWidth); } else { $borderAttribute = ""; } if ($imgArrayCaptionMode) { $textCaption = $caption; $spanTag = sprintf("%s", $textCaption); } else { $textCaption = ""; $spanTag = ""; } if ($imgArrayDivMode) { // generate div tags around the image an include the caption (if any) printf("%s", $classAttribute, $filename, $sizeAttributes, $borderAttribute, $altAttribute, $titleAttribute, $spanTag); } else { printf("%s", $classAttribute, $filename, $sizeAttributes, $borderAttribute, $altAttribute, $titleAttribute, $textCaption); } ?>