Simple image rotation script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico-t
    emperor of my world
    • Aug 2004
    • 29903

    #1

    Simple image rotation script

    Been looking all over for this. But all i see is script rotating 1 image.

    i've got about a 100 images.
    I want to display for example 5 of these images randomly every page load (without doubles).
    If some images get repeated onload it doesnt matter, as long as there aren't doubles in every row of 5.
    All the pics need to link to a unique url too.

    Suggestions?
  • DWB
    Registered User
    • Jul 2003
    • 31779

    #2
    WOJ has a simple script that will do exactly that.

    Comment

    • Oracle Porn
      Affiliate
      • Oct 2002
      • 24433

      #3
      Option 1: http://ma.tt/scripts/randomimage/

      Option 2: http://randaclay.com/tips-tools/mult...ge-php-script/

      Option 3:
      Code:
      <?php
      define('BASE_DIR', '/var/www/mysite/images/'); // Where are your images?
      define('IMAGE_LIMIT', 10); // how many images to show?
      
      $files     = glob(BASE_DIR.'*');
      $images = Array();
      
      if(empty($files)) {
          die("Hey, dude, there are no files. What happened?");
      }
      
      foreach($files as $v) {
          if(is_dir($v)) {
              foreach($files as $v) { // This should be a function which can loop through further sub directories if needed
                  //get exif image type, if it's an image append to array
                  $images[] = $v;
              }
          } else {
              $images[] = $v;
          }
      }
      
      shuffle($images); // Randomise the array
      for($i=0;$i<IMAGE_LIMIT;$i++) {
          $img = $images[$i];
          if(empty($img)) {
              break; // Might be best doing a foreach above whilst keeping $i as a loop counter and breaking the loop once you reach the limit
          }
          $img_src = end(explode("/", $img));
          echo "<img src='{$img_src}'/>";
      }


      Comment

      • seeandsee
        Check SIG!
        • Mar 2006
        • 50945

        #4
        Originally posted by Oracle Porn
        Option 1: http://ma.tt/scripts/randomimage/

        Option 2: http://randaclay.com/tips-tools/mult...ge-php-script/

        Option 3:
        Code:
        <?php
        define('BASE_DIR', '/var/www/mysite/images/'); // Where are your images?
        define('IMAGE_LIMIT', 10); // how many images to show?
        
        $files     = glob(BASE_DIR.'*');
        $images = Array();
        
        if(empty($files)) {
            die("Hey, dude, there are no files. What happened?");
        }
        
        foreach($files as $v) {
            if(is_dir($v)) {
                foreach($files as $v) { // This should be a function which can loop through further sub directories if needed
                    //get exif image type, if it's an image append to array
                    $images[] = $v;
                }
            } else {
                $images[] = $v;
            }
        }
        
        shuffle($images); // Randomise the array
        for($i=0;$i<IMAGE_LIMIT;$i++) {
            $img = $images[$i];
            if(empty($img)) {
                break; // Might be best doing a foreach above whilst keeping $i as a loop counter and breaking the loop once you reach the limit
            }
            $img_src = end(explode("/", $img));
            echo "<img src='{$img_src}'/>";
        }
        Hi, thanks for this code. Can you point me to some good php tutorial site, i have experience in programing (c, c++, basic, even pascal) but i need good site to learn some step by step php code?
        Last edited by seeandsee; 12-30-2012, 03:51 AM.
        BUY MY SIG - 50$/Year

        Contact here

        Comment

        • just a punk
          So fuckin' bored
          • Jun 2003
          • 32393

          #5
          Assuming that all image URL's are stored in "images.txt" (one URL per line), the code will be as simple as this:
          Code:
          $images = file('images.txt');
          shuffle($images);
          for ($i = 0; $i < min(5, count($images)); $i++) {
              echo '<img src="' . trim($images[$i]) . '" />';
          }
          Obey the Cowgod

          Comment

          • nico-t
            emperor of my world
            • Aug 2004
            • 29903

            #6
            Originally posted by Oracle Porn
            Option 1: http://ma.tt/scripts/randomimage/

            Option 2: http://randaclay.com/tips-tools/mult...ge-php-script/

            Option 3:
            Code:
            <?php
            define('BASE_DIR', '/var/www/mysite/images/'); // Where are your images?
            define('IMAGE_LIMIT', 10); // how many images to show?
            
            $files     = glob(BASE_DIR.'*');
            $images = Array();
            
            if(empty($files)) {
                die("Hey, dude, there are no files. What happened?");
            }
            
            foreach($files as $v) {
                if(is_dir($v)) {
                    foreach($files as $v) { // This should be a function which can loop through further sub directories if needed
                        //get exif image type, if it's an image append to array
                        $images[] = $v;
                    }
                } else {
                    $images[] = $v;
                }
            }
            
            shuffle($images); // Randomise the array
            for($i=0;$i<IMAGE_LIMIT;$i++) {
                $img = $images[$i];
                if(empty($img)) {
                    break; // Might be best doing a foreach above whilst keeping $i as a loop counter and breaking the loop once you reach the limit
                }
                $img_src = end(explode("/", $img));
                echo "<img src='{$img_src}'/>";
            }
            I'm using the 3rd option now, and added some code to display unique links for every image begin shown, between these last lines of code:
            Code:
                $img_src = end(explode("/", $img));
                echo "<img src='{$img_src}'/>";
            }
            \

            I have added some lines to assign imagename to the url for each pic like i was planning to:
            Code:
                $img_src = end(explode("/", $img));
                $imgid = basename($img, ".jpg");
                echo "<a href='http://www.mydomain/id=$imgid'><img src='{$img_src}'/></a>";
            } 
            ?>
            And it works exactly like i wanted! Thanks a lot for helping me out

            Comment

            • Oracle Porn
              Affiliate
              • Oct 2002
              • 24433

              #7
              you mind posting the entire edited code?
              thanks


              Comment

              • Oracle Porn
                Affiliate
                • Oct 2002
                • 24433

                #8
                Originally posted by seeandsee
                Hi, thanks for this code. Can you point me to some good php tutorial site, i have experience in programing (c, c++, basic, even pascal) but i need good site to learn some step by step php code?
                I use this site


                Comment

                • nico-t
                  emperor of my world
                  • Aug 2004
                  • 29903

                  #9
                  one thing i noticed. About the above code.

                  When you have your scriptcode.php in the same folder as the images, the rotator sometimes grabs the .php file too.

                  When i put the scriptcode.php in a parent folder:
                  /mysite/script/scriptcode.php
                  And the images in:
                  /mysite/script/images/image.jpg

                  The script looks for the images in /mysite/script/ like it doesn't see the images folder.
                  I have put the correct image path in the script. Think it has something to do with the explode code?

                  Comment

                  • nico-t
                    emperor of my world
                    • Aug 2004
                    • 29903

                    #10
                    fixed the above by just adding images dir before the img src output.


                    Originally posted by Oracle Porn
                    you mind posting the entire edited code?
                    thanks
                    ofcourse, here it is.
                    script is at:
                    mysite.com/script/script.php
                    Images in:
                    mysite.com/script/images/

                    Code:
                    Code:
                    <?php
                    define('BASE_DIR', '/home/user/domains/mysite.com/public_html/script/images/'); // Where are your images?
                    define('IMAGE_LIMIT', 5); // how many images to show?
                    
                    $files     = glob(BASE_DIR.'*');
                    $images = Array();
                    
                    if(empty($files)) {
                        die("Hey, dude, there are no files. What happened?");
                    }
                    
                    foreach($files as $v) {
                        if(is_dir($v)) {
                            foreach($files as $v) { // This should be a function which can loop through further sub directories if needed
                                //get exif image type, if it's an image append to array
                                $images[] = $v;
                            }
                        } else {
                            $images[] = $v;
                        }
                    }
                    
                    shuffle($images); // Randomise the array
                    for($i=0;$i<IMAGE_LIMIT;$i++) {
                        $img = $images[$i];
                        if(empty($img)) {
                            break; // Might be best doing a foreach above whilst keeping $i as a loop counter and breaking the loop once you reach the limit
                        }
                        $img_src = end(explode("/", $img));
                        $imgid = basename($img, ".jpg");
                        echo "<a href='http://www.mysite.com/id=$imgid'><img src='images/{$img_src}'/></a>";
                    } 
                    ?>
                    Takes filename of pic (which is also the url ID) to link every pic to its own ID page.

                    Comment

                    • Barry-xlovecam
                      It's 42
                      • Jun 2010
                      • 18083

                      #11
                      nvm ..... read the image part
                      Last edited by Barry-xlovecam; 12-30-2012, 07:09 AM.

                      Comment

                      • fitzmulti
                        I Like Depth Of Field!
                        • Jan 2003
                        • 14861

                        #12
                        Neat code to have. Thanks!


                        www.SexyGirlsCash.com


                        CONTACT // FITZMULTI AT GMAIL.COM //
                        {Please include a message so I know you are from GFY! I get too many spam "add requests"!}

                        Comment

                        Working...