to previous topic Print topic to next topic

 Root :: Developments :: Tutorials and programming :: Script for Random Images..
 Moderated by:
 
  Bottom  Script for Random Images..
viking Posted: 26.05.2006, 12:09
Noob
Noob
avatar

registered: Jul 02, 2003
Posts: 12

Status:  offline
last visit: 13.03.08
I get this code from PN Community.. just asking here too.. :D

The problem is, it randomize pictures just one time.. and stop at the last picture.. :(

Code
  1. <?php
  2. /*
  3.     By Matt Mullenweg > http://photomatt.net
  4.     Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
  5.     Latest version always at:
  6.     http://photomatt.net/scripts/randomimage
  7. */
  8.  
  9. // Make this the relative path to the images, like "../img" or "random/images/".
  10. // If the images are in the same directory, leave it blank.
  11. $folder = 'images/banners/';
  12.  
  13. // Space seperated list of extensions, you probably won't have to change this.
  14. $exts = 'jpg jpeg png gif';
  15.  
  16. $files = array(); $i = -1; // Initialize some variables
  17. if ('' == $folder) $folder = './';
  18. $handle = opendir($folder);
  19. $exts = explode(' ', $exts);
  20. while (false !== ($file = readdir($handle))) {
  21.     foreach($exts as $ext) { // for each extension check the extension
  22.         if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
  23.             $files[] = $file; // it's good
  24.             ++$i;
  25.             }
  26.         }
  27.     }
  28. closedir($handle); // We're not using it anymore
  29. mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
  30. $rand = mt_rand(0, $i); // $i was incremented as we went along
  31.  
  32. header('Location: '.$folder.$files[$rand]); // Voila!
  33. ?>
Top  viking send PM
 
Chestnut Posted: 26.05.2006, 17:28
Site Admin
avatar

registered: Jun 02, 2002
Posts: 1320

Status:  offline
last visit: 07.02.08
Not sure I understand when you say it will randomize "just one time"... Is this thing supposed to be randomizing without reload ?

From what I can see, it should show a random image on each reload.


Chestnut ! Cool
Site Admin
Top  Chestnut send PM Homepage
 
viking Posted: 27.05.2006, 01:51
Noob
Noob
avatar

registered: Jul 02, 2003
Posts: 12

Status:  offline
last visit: 13.03.08
Mm.. thanks for reply..
Yes.. it show a random image on each reload.. but stuck at the last image.. It`s 3 images there, it showing different image for each reload, but only showing same image after more than 3 time reloading..

That`s what i`m trying ;

1. Make one simple new module that contain this script.. (Showing random picture)..

2. SO I download Markwest module --http://www.markwest.me.uk/module-DynaMOD.phtml--

3. I change the template file "dynamod_user_main.htm" -- by write this **img scr ="random.php"**

Or.. maybe better way.. ??

---
Sorry for my bad English.. :(



edited by: viking, May 26, 2006 - 12:53 PM
Top  viking send PM
 
Chestnut Posted: 27.05.2006, 13:36
Site Admin
avatar

registered: Jun 02, 2002
Posts: 1320

Status:  offline
last visit: 07.02.08
mmm Can't help much on the DynaMOD thing as I never played with it... (Might ask on http://markwest.me.uk/ for that)...

But from what you are saying... I guess it could be right.

One thing though is that caching could be a reason why you see often the same image...

Maybe use
Code
  1. <!--[nocache]-->your image<!--[/nocache]-->




Chestnut ! Cool
Site Admin
Top  Chestnut send PM Homepage
 
viking Posted: 17.09.2006, 06:33
Noob
Noob
avatar

registered: Jul 02, 2003
Posts: 12

Status:  offline
last visit: 13.03.08
Mm.. yes it`s me again.. icon_smile

This is another script that work too..
But, can we get the images files from other site.. for example, I have account at Photobucket.com.. so just want the script to get the images form there.. exp : all my images here - http://s95.photobucket.com/albums/l159/foto/aman/

Thank in advance..

Code
  1. if(!is_dir($directory)){
  2.         $directory = 'HOT';
  3.     }
  4.    
  5.     $images = array();
  6.     $ignore = array('.','..','.DS_Store','index.html');
  7.     $dir = opendir($directory);
  8.     while(false !== ($file = readdir($dir))) {
  9.         if(in_array($file,$ignore)) continue;   
  10.         $images[] = $file;
  11.     }
  12.     closedir($dir);
  13.    
  14.     $file = $directory.'/'.$images[mt_rand(0,count($images)-1)];
  15.    
  16.     list($width, $height, $type, $attr) = getimagesize($file);
  17.    
  18.     $return = array('src'=>$file,
  19.                     'width'=>$width,
  20.                     'height'=>$height,
  21.                     'type'=>$type,
  22.                     'attr'=>$attr);
  23.    
  24.     if (isset($params['assign'])) {
  25.         $smarty->assign($params['assign'], $return);
  26.         return;
  27.     } else {   
  28.         return $file;
  29.     }
Top  viking send PM
 
Chestnut Posted: 25.09.2006, 10:49
Site Admin
avatar

registered: Jun 02, 2002
Posts: 1320

Status:  offline
last visit: 07.02.08
You probably must know thde images in advance. Since you can't browser the content of a remote folder, if you know all the images name, you build yourself the array $images instead of using the opendir and all that part.

icon_wink



Chestnut ! Cool
Site Admin
Top  Chestnut send PM Homepage
 



Powered by pnForum Version 2.0.1
DarkMindZ