no duplicate image in random script problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perik78
Forum Newbie
Posts: 8
Joined: Mon Dec 28, 2009 5:20 pm

no duplicate image in random script problem

Post by perik78 »

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:

Code: Select all

<?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;
}
?>
and for calling the images (the long scripts is because I got 25 pictures that is displayed):

Code: Select all

<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>
any suggestions?

thanks a lot!

Perik
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: no duplicate image in random script problem

Post by JakeJ »

You need two arrays:

$imagelist = array();
$usedimages = array(); (name them as you wish of course)

$imagelist should contain a complete directory listing of your images.
On each pass, run a comparison between $imagelist and $usedimages. Unset anything in $imagelist found in $usedimages. Then your image generator will only be able to choose a new image.

Be sure to create code for the eventuality of running out of images even if you don't think it will happen.

I hope that helps.
Post Reply