Here you go ... a little image rotation snippet !

Discussion in 'PHP' started by iatbm, Jun 16, 2006.

  1. #1
    You can use this for image rotation next to your adsense ads so when your visitors are browsing through your site they will se different image every time they refresh.

    It is pretty self explanatory how to use this I hope :D

    
    <?php
    $path_to_images = "images/";  // path to your images
    $default_img = "turtle.gif";  // image to display if directory listing fails
    
    function getRandomImage($path, $img) {
       if ( $list = getImagesList($path) ) {
           mt_srand( (double)microtime() * 1000000 );
           $num = array_rand($list);
           $img = $list[$num];
       }    return $path . $img;
    }
    
    function getImagesList($path) {
       $ctr = 0;
       if ( $img_dir = @opendir($path) ) {
           while ( false !== ($img_file = readdir($img_dir)) ) {
               // can add checks for other image file types here
               if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) {
                   $images[$ctr] = $img_file;
                   $ctr++;
               }
           }
           closedir($img_dir);
           return $images;
       }    return false;
    }
    ?> 
    
    Code (markup):

     
    iatbm, Jun 16, 2006 IP
  2. use

    use Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    very interesting thank you
     
    use, Jun 19, 2006 IP
  3. SFOD_D223

    SFOD_D223 Peon

    Messages:
    4,512
    Likes Received:
    174
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can this be used for other contextual ad services?
     
    SFOD_D223, Jun 20, 2006 IP
  4. iatbm

    iatbm Prominent Member

    Messages:
    5,151
    Likes Received:
    352
    Best Answers:
    0
    Trophy Points:
    360
    #4
    With a little modifications you can use it for anything, but this one is for rotating images in specified folder. Next to the image you can have whatever you want :D
     
    iatbm, Jun 20, 2006 IP
    SFOD_D223 likes this.
  5. SFOD_D223

    SFOD_D223 Peon

    Messages:
    4,512
    Likes Received:
    174
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Without sounding like a total newb..where exactly would i insert your script? what directory/file if it's not too much of a bother.
     
    SFOD_D223, Jun 20, 2006 IP
  6. iatbm

    iatbm Prominent Member

    Messages:
    5,151
    Likes Received:
    352
    Best Answers:
    0
    Trophy Points:
    360
    #6
    you just save it as something.php and then in your home page just call it with php include function ;)
     
    iatbm, Jun 20, 2006 IP