no duplicate image in random script problem

Discussion in 'PHP' started by perik, Apr 13, 2010.

  1. #1
    Hi!

    I have a randomscript which both makes a random image from a folder and creates a random class. Sometimes it duplicates the images and my question is:

    How do I make the images to just be displayed once?

    the script is:
    <?php
    /* The default folder with images */
    //$settings['img_folder'] = 'plugins/project_menu/images/thumbs/';
    
    /* File types (extensions) to display */
    $settings['img_ext'] = array('.jpg','.gif','.png');
    
    /*
       How to display the images?
       0 = print just the image path (for includes), like: images/test.jpg
       1 = redirect to the image, when using: <img src="randim.php" />
    */
    $settings['display_type'] = 0;
    
    /* Allow on-the-fly settings override? 0 = NO, 1 = YES */
    $settings['allow_otf'] = 1;
    
    
    /*******************************************************************************
    *  DO NOT EDIT BELOW...
    *
    *  ...or at least make a backup before you do!
    *******************************************************************************/
    
    /* Override type? */
    if ($settings['allow_otf'] && isset($_GET['type']))
    {
    	$type = intval($_GET['type']);
    }
    else
    {
    	$type = $settings['display_type'];
    }
    
    /* Override images folder? */
    if ($settings['allow_otf'] && isset($_GET['folder']))
    {
    	$folder = htmlspecialchars(trim($_GET['folder']));
        if (!is_dir($folder))
        {
        	$folder = $settings['img_folder'];
        }
    }
    else
    {
    	$folder = $settings['img_folder'];
    }
    
    /* Make sure images fodler ends with an '/' */
    if (substr($folder,-1) != '/')
    {
    	$folder.='/';
    }
    
    /* Get a list of all the image files */
    $flist = array();
    foreach($settings['img_ext'] as $ext)
    {
        $tmp = glob($folder.'*'.$ext);
        if (is_array($tmp))
        {
        	$flist = array_merge($flist,$tmp);
        }
    }
    
    /* If we have any images choose a random one, otherwise select the "noimg.gif" image */
    if (count($flist))
    {
    	$src = $flist[array_rand($flist)];
    }
    else
    {
    	$src = 'noimg.gif';
    }
    
          
    /* Output the image according to the selected type */
    if ($type)
    {
    	header('Location:'.$src);
        exit();
    }
    else
    {
    //	echo $src;
    }
    ?>
    PHP:
    and for calling the images (the long scripts is because I got 25 pictures that is displayed):
    
    <body>
    
    <?php
    // This is for making a random class
    class UniqueRand{
      var $alreadyExists = array();
    
      function uRand($min = NULL, $max = NULL){
        $break='false';
        while($break=='false'){
          $rand=mt_rand($min,$max);
    
          if(array_search($rand,$this->alreadyExists)===false){
            $this->alreadyExists[]=$rand;
            $break='stop';
          }else{
      //      echo " $rand already!  ";
        //    print_r($this->alreadyExists);
          }
        }
        return $rand;
      }
    }
    $rand=new UniqueRand(); 
    
    
    ?>
    <?php
    /* this is for making a random image from the folder 
    plugins/project_menu/images/thumbs/ hyperlinked to 
    plugins/project_menu/images/pictures/ */
    
    $_GET['type']=0; 
    
    $_GET['folder']='plugins/project_menu/images/pictures/';
    
    
    // new random image1;
    include 'randim.php';
    //now you have large image in $src;
    // replace folder name assuming filename is the same
    $small = str_replace('plugins/project_menu/images/pictures/','plugins/project_menu/images/thumbs/',$src);
     ?>
    <ol>
    <li><div class="scrollbox"> 
    <div class="<?="randompacering". $rand->uRand(1,25).""?>"><a class="fancy" rel="fancy" href="<?=$src;?>"><img border='0' height='40px' src="<?=$small;?>"></a></div></p>
    </li>
    
    <?php
    // new random image2;
    include 'randim.php';
    $small = str_replace('plugins/project_menu/images/pictures/','plugins/project_menu/images/thumbs/',$src);
     ?>
    <li>
    <div class="<?="randompacering". $rand->uRand(1,25).""?>"><a class="fancy" rel="fancy" href="<?=$src;?>"><img border='0' height='40px' src="<?=$small;?>"></a></div></p>
    </li>
    
    </ol>
    </body>
    </html>
    PHP:
    any suggestions?

    thanks a lot!

    Perik
     
    perik, Apr 13, 2010 IP