Need some advice on rotating images

Discussion in 'PHP' started by fadetoblack22, Oct 27, 2008.

  1. #1
    I am going to use this script on my site:

    http://www.alistapart.com/d/randomizer/rotate.txt

    I have 10 different sections on my site which have rotating images. They can't be mixed up so I am going to use the code 10 times in 10 different folders.

    I am thinking this could be a bit bulky. The code isn't that long, but is there an easier way to do this?

    Or am I wrong and it will load quite fast?

    thanks for the help.
     
    fadetoblack22, Oct 27, 2008 IP
  2. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #2
    I've tried it with 3 images and it seems to work ok.

    I have 3 folders each with three images in them.

    I wonder if someone can help me edit the code slightly.

    At the moment it always selects the images in order of file name in the folder rather than properly random. Can someone edit the code for me to make it select a random one?

    Also I want to add a variable so that the images are only changed every hour or 12 hours depending on the value needed.
     
    fadetoblack22, Oct 27, 2008 IP
  3. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It will load quite fast, although the script should be improved if your server is under high load since it doesn't support caching. (can't believe alistapart features such largely unoptimized code snippets.. ["version 2.2"... omg])

    If you wish to do this, take a look at the "ETag" header and the HTTP 304 "not modified" status code. (Hint: the md5sum of a filename can directly be used as an ETag, for example)

    If you can't figure it out, post back and I'll dig out some code of mine
     
    keyaa, Oct 27, 2008 IP
  4. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well here's the code for the caching, I kind of wanted to look at it again ;)

    function displayfile($path) {
      if(file_exists($path)) {
        $etag = md5($path);
        $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
          stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
          false;
        header("ETag: $etag");
        header('Content-Length: '.filesize($path));
        header('Cache-Control: private, must-revalidate');
        header('Pragma: private');
        if ($if_none_match != $etag) {
          readfile($path);
        } else {
          header('HTTP/1.0 304 Not Modified');
        }
      } else {
        header('HTTP/1.1 404 Not Found');
        header('Status: 404 Not Found');
      }
      exit();
    }
    PHP:
    Note:
    - needs Content-Type header first
    - since this was written for a more secure webapp (no caching of images allowed for spiders, images won't show after logoff), you probably want to change/remove the Pragma/Cache-Control headers
     
    keyaa, Oct 27, 2008 IP
  5. Optional

    Optional Peon

    Messages:
    123
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think this should sort out your radomize issue Fadetoblack


    change this bit;

    if (count($fileList) > 0) {
    $imageNumber = time() % count($fileList);
    $img = $folder.$fileList[$imageNumber];
    }


    To this;

    if (count($fileList) > 0) {
    shuffle($fileList);
    $imageNumber = time() % count($fileList);
    $img = $folder.$fileList[$imageNumber];
    }
     
    Optional, Oct 27, 2008 IP
  6. Optional

    Optional Peon

    Messages:
    123
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The best way to do this would be incorporating it in keyaa's caching code.


    This script was used in a wordpress theme I used recently... i dont like it personally, because it uses a php script to display the images... when it should use the php script to write the plain old html code for the link.

    Means everytime every image is lodded it comes off your server, rather than previously viewed images from the visitors cache.


    It sounds like you can read enough PHP to work this simple script out, maybe try write the snippet yourself.

    I do things weird ways... but for what it sounds like you are doing, I'd write a few lines of code that is called on everypage load that will re-write a seperate file with your image's html which is included in the page whereever you need it... and have the first bit of code simpl check the time stamp of the include file each time and update it the first time the page is viewed after your time period.

    The "complex" part of this bit of code is the bit to read the list of files from the folder. I'd do away with that and, and for soemthing as one-off trivial as this, probably have my image file names and any stuff like alt descriptions and links strored in a simple csv file and generate your insert code from that
     
    Optional, Oct 27, 2008 IP
  7. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You're right, the images are always being piped through php in my code, this is because it was a necessity to only allow access to images to logged-in users in my project. Could/should be changed for noncritical content/webapps.
     
    keyaa, Oct 27, 2008 IP
  8. Optional

    Optional Peon

    Messages:
    123
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    oops sorry keyaa.... I missed the fact it is your code. (sorry for the neg comment)

    I am sure you can help our friend better than me :)
     
    Optional, Oct 27, 2008 IP
  9. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #9
    Hey again "optional". thanks for the randomize code change.

    keyaa, I don't understand enough to put your cache code into the script.
    I can't write or understand php at all lol. I just guess what bits mean.

    If this script is going to give me problems with caching, feel free to suggest an existing one that will work better :)
     
    fadetoblack22, Oct 27, 2008 IP