<?
 
require_once ( dirname ( __FILE__ ) . "/Html.php" );
 
 
/**
 
 * RotateImage class reads a directory for a matching list of images
 
 * and then displays a random image. If you then click on the image
 
 * it will rotate to the other images that you have
 
 * Author - Brett Dutton
 
 */
 
class RotateImage {
 
 
    var $url;  // This is the Base URL. This is used to construct the image URL
 
    var $path; // This is the path to the Base location 
 
    var $dir;  // 
 
    var $mask;
 
 
    function RotateImage ( $url, $path, $dir, $mask ) {
 
        $this->url  = $url;
 
        $this->path = $path;
 
        $this->dir  = $dir;
 
        $this->mask = $mask;
 
    }
 
 
    function toHtml ( ) {
 
        $location = $this->path . "/" . $this->dir;
 
 
        $arr = array ();
 
 
        if ( $dh = opendir ( $location ) ) {
 
            while ( ( $fileName = readdir ( $dh ) ) !== false ) {
 
                $file = $location . "/" . $fileName;
 
                if ( is_file ( $file ) && preg_match ( $this->mask, $fileName ) ) {
 
                    $arr[] = $fileName;
 
                }
 
            }
 
        }
 
        if ( ( $cnt = count ( $arr ) ) == 0 ) return ( "Error, no images found" );
 
 
        // Get a random image
 
        $img = $arr[rand ( 0, $cnt - 1 )];
 
 
        // Create a variable name for labeling the javascript variables
 
        $var = str_replace ( ".", "_", $img );
 
 
        // create the javascript for the image rotation
 
        $js = "";
 
        $js .= "var cur$var = 0;\n";
 
        $js .= "var arr$var = new Array ();\n";
 
        for ( $i=0; $i<count ( $arr ); $i++ ) {
 
            $js .= "arr{$var}[{$i}] = '" . $this->url . "/" . $this->dir . "/" . $arr[$i] . "';\n";
 
        }
 
        $js .= "function clickHandle$var () {\n";
 
        $js .= "   document['$var'].src = arr{$var}[cur$var++];\n";
 
        $js .= "   if ( cur$var >= arr$var.length ) cur$var = 0;\n";
 
        $js .= "}";
 
 
        $imgHtml = Html::img ( $this->url . "/" . $this->dir . "/" . $img, "border=0 name=\"$var\"" );
 
        $hrefHtml = Html::href ( "javascript:clickHandle$var()", $imgHtml, "title=\"Click to see more images\"" );
 
        return ( Html::javaScript ( $js ) . $hrefHtml );
 
    }
 
}
 
?>
 
 |