Comments on A Better Image Rotator

151 Reader Comments

Back to the Article
  1. This is on the face of it an excellent idear to have a php script that can be included in a php file that rotates various images round each time you load up the page….

    However I have tried to get it to work a number of times and failed completely.. the script uses an ini file and sometimes shows a jpg file I have on the server but somtimes fails to load a picture and instead just shows a tiny red cross and no image….

    It is very disappointing that it appears not to work correctly
    I would like to accept e mail about this in an attempt to correct this.. but feel that there must be several things wrong, either with the format of the ini file or the rotator.php script or both…........
    Help>>>>...???

    Copy & paste the code below to embed this comment.
  2. it’s extremely slow… been testing the script, watching the ‘activity viewer’ as it downloads the test-page, sometimes it loads fast, but most of the time the download of the page grinds to a halt when it starts downloading the rotator.php file

    Copy & paste the code below to embed this comment.
  3. will it work with text not images?

    Copy & paste the code below to embed this comment.
  4. This script will only use the first image listed in the .ini file.  I tried messing around with rotation.php but nothing worked. 

    From what I can see, either the ini isnt working correctly, or the rand() command isnt working for some reason.

    Can anyone help me here?

    Copy & paste the code below to embed this comment.
  5. well i know whats breaking it, but not sure how to fix it ;-)

    the problem is the .ini files. if you have an url with a querystring appended to it, the array that parses the ini trips over and everything explodes in a big flaming fireball.

    please please mr benjamin can you implement querystring support?

    Copy & paste the code below to embed this comment.
  6. well, i fixed it. if your url has a querystring in it. wang some quotes around the ini entry.

    eg;

    url   = “think.php?id=43&category=life”

    thanks for great little script benjamin.

    Copy & paste the code below to embed this comment.
  7. I can get this to work, but I am having trouble with using it in sub-directories. Ideally, I want to be able to use this with some variables.

    For example, I want to set a variable for the root directory so that the “rotator.php” can be accessed by going to to the root level ($root/rotator.php). However, when I try to do this, I get an error with the showimage() function (Fatal error: Call to undefined function: showimage() in ...) 

    Any ideas of how to fix this?

    Copy & paste the code below to embed this comment.
  8. Is there a way to rotate images on a certain interval using only PHP? or at least clean JS? I was thinking of implementing a session ID… but that requires session code on each page. Any suggestions?

    Copy & paste the code below to embed this comment.
  9. it is possible to create several links at different places from the loaded image ?

    Any ideas of how to do this?

    Copy & paste the code below to embed this comment.
  10. i’m trying to add the ‘better image rotator’ from

    http://www.alistapart.com/comments/betterrotator/

    on to www.rapnews.co.uk

    as you can see, the command in my sidemenu is showing as code, and not the image.

    Copy & paste the code below to embed this comment.
  11. I found a way to put several links at different places from the loaded image. I used CSS Style sheet.
    For the moment, my new site is visible at :
    http://web.design.creation.free.fr/azurid/
    As you can see, “image rotator” generates a random image on my first web page. And one can click on various links.
    If you are interesting by this. Contact me.
    Flip flop.

     

    Copy & paste the code below to embed this comment.
  12. I’m getting the error

    Unable to read ini file.

    Can anyone help? It’s got the correct code inside and is in the correct folder.

    Copy & paste the code below to embed this comment.
  13. can we see your script inside the ini file ?
    and the path you used to call it ?

    Copy & paste the code below to embed this comment.
  14. content of ini:

    [Headnod]
    src   = images/headnod.jpg
    alt   = Headnod.co.uk
    url   = http://www.headnod.co.uk/
    title = Headnod

    [Tony]
    src   = images/advert.jpg
    alt   = Advert
    url   = http://www.rapnews.co.uk/
    title = Advert


    The path is as instructed on this site’s implementation instructions.

    Copy & paste the code below to embed this comment.
  15. 1/ I suppose that the content of “.ini” is written like this :

    [Headnod]
    src = images/headnod.jpg
    alt = Headnod.co.uk
    url = http://www.headnod.co.uk/
    title = Headnod

    2/ Are you sure that you call the right “.ini” file in the rotator.php script ? :

    <?php
      $IMG_CONFIG_FILE = ‘images.ini’;
      .........................

    Copy & paste the code below to embed this comment.
  16. 1/ I suppose that the content of “?.ini”? is written like this :


    [Headnod]

    src = images/headnod.jpg

    alt = Headnod.co.uk

    url = http://www.headnod.co.uk/

    title = Headnod

    Copy & paste the code below to embed this comment.
  17.  
    Just for saying that the exemple is now visible at :

    http://www.azurid.com/

    As you can see, “image rotator”? generates a random image on my first web page. And one can click on various links. If you are interesting by this. Contact me. Flip flop.

    METTRE PLUSIEURS LIENS SUR L’IMAGE chargée par rotator !!!

      Juste pour dire que le lien qui montre l’exemple a changé. L’exemple est maintenant visible sur la page d’accueil de mon site (une fois sur la page, réactualisez le site et l’image change) :

      http://www.azurid.com/

     

    Copy & paste the code below to embed this comment.
  18. The first link (a better rotator) shows an error message from Rails

    Copy & paste the code below to embed this comment.
  19. I did not see a solution to the previous post on this problem, and would like to add that I get the folowing error message:
    Warning: Error parsing images.ini on line 3 in Unknown on line 0

    this displays on the line above the image that displays.

    Any help appreciated.

    Copy & paste the code below to embed this comment.
  20. Hrumph- turns out it was the exclaimation (!) points interrupting the parsing of the .ini file (I had included them in the alt’s)- this is new to me.

    Copy & paste the code below to embed this comment.
  21. Your script is good. But when i used it, I ran into 2 problems. It was slow in retrieving each random image. But it also created firewall problems on two different machines. For this reason i had to stop using it. I currently use the solution given at http://www.tutorialized.com/tutorial/Simple-Image-Rotater/790. This is a block of code you insert into your main php file and does not reference an external file. This seems much quicker than the ala solution and also have had no firewall problems. However, I want to thank you sincerely for introducing me to the concept of image rotation.

    Copy & paste the code below to embed this comment.
  22. I can’t seem to get the script to parse more than two sections. Is this a limitation of the php function or am I doing something wrong?

    Copy & paste the code below to embed this comment.
  23. Is there a way to modify this script to attach specific captions to an image?

    Copy & paste the code below to embed this comment.
  24. The rotator script works beautifully.  However I want to rotate .swf header movies on my home page.  I guess I would need to change the $extLit value, but what would that be to display random .swf movies?

    Thanks

    Copy & paste the code below to embed this comment.
  25. I too would be interested in a way to display the title under the image. As it sits now, it’s not useful for my application.

    Thanks

    Copy & paste the code below to embed this comment.
  26. Unfortunately, our servers at work do not support PHP. Can someone post a link to a non-PHP method. Preferabley ColdFusion or Javascript. Thanks!

    Copy & paste the code below to embed this comment.
  27. An exemple of image rotator :
    http://groupe.argentis.free.fr

    Copy & paste the code below to embed this comment.
  28. Microsoft Visual Studio 2005,which has got an component called “AdRotator”.I really like it,it works as well as the author’s work.Thanks to the author, now I know how it works.

    Copy & paste the code below to embed this comment.
  29. i put all the code in a folder called artist, and when i say <?php include(‘artist/index.php’) ?> nothing happens….please help im new to php!!

    Copy & paste the code below to embed this comment.
  30. Yes, adding a caption is possible. First, add a caption variable to the script:

    $caption = $images[$img][‘caption’];

    Next, edit the script to incorporate the caption variable into the HTML ouput. Something like this:

    if ($caption) {
    printf('%s.', $caption);
    }

    Last, add a caption line to each block in the .ini file.

    Copy & paste the code below to embed this comment.
  31. Not sure why, but the first part of that PHP code got a little munged in transit. I’ll take another stab; perhaps the ALA admin would be good enough to help if it comes out wrong again? :).

    #get the caption

    $caption = $images[$img]['caption'];
    Copy & paste the code below to embed this comment.
  32. .

    I am using Gallery 1.X.

    Right now everything is in this path, images, rotator, index, .ini, ... Everything. ...

    http://www.classannual.com/php/gallery/rotateJax/

    That works.

    But I just cannot get it included in the actual gallery pages. ... On this page I have a random image. ...

    http://www.classannual.com/php/gallery/

    I would like to replace that Random image with a RandomImagePlusLink, like this BETTER IMAGE ROTATOR tool generates in rotateJax.

    Since Gallery has a bunch of includes, The index is in the gallery folder, where would I place the folder with my images, my ini file, my rotator, and what would my php include say? ...

    How do I address paths in PHP?

    Jack

    Copy & paste the code below to embed this comment.
  33. .

    http://www.classannual.com/php/gallery/

    The image in the upper right is random, and it links. ...

    There are about 2600 image/link pairs in the ini file right now. ...

    It seems to work for me!

    Copy & paste the code below to embed this comment.
  34. I can get the gallery front page to work, but when I bounce off the page and navigate to another gallery page with a set of albums, then try to go back to the top page, I get an error. ...

    I am not a wood nymph, but I think there is a troll in my machine.

    Copy & paste the code below to embed this comment.
  35. I am hoping that someone can help me figure out how to have the links open in a new browser window.  Does anyone know where I need to put the “target=_blank” command in the ini or the configuration file?

    Copy & paste the code below to embed this comment.
  36. I have been using your ROTATOR script for about a year and I really enjoy its performance. I have approximately 200 images on my web site that I rotate. Each image is linked to a synopsis of a book. I was using one images.ini file but found that many of the images repeated before all of the images were displayed. Many of the images are never displayed. I broke the images file into 12 different files and that improved the performance but I still have the problem that images repeat before all are displayed.

    Is there a method that will insure that all images are displayed before any one is repeated?

    I have also tried to implement the CAPTION mod but I could not get it to work. Thanks for any help you can provide.

    Thanks
    Buddy

    Copy & paste the code below to embed this comment.
  37. A friend refered me to your post. I have used a similar block of code for a random image module. //Spit it out
    $limit=count($image);$limit—;
    $randnum=rand(0,$limit);
    // $size=getimagesize(”$directory$image[$randNum]”);

    $filename = “$directory$image[$randnum]”; // Slipstream our content into the template
    eval(’$home[$mods[\‘modid\’]][\‘content\’] = ”’ . fetch_template(‘adv_portal_random_video’) . ‘“;’); ?>

    I’d like to know how I can make it call the most recently uploaded image, rather than a random one.

    Copy & paste the code below to embed this comment.
  38. Can anyone tell me what I am doing wrong? I have added the caption mod but it wont work.

    I also added caption to the ini file like this.

    [11]
    src   = /images_used/067188770X_small.jpg
    alt   = Betty Crocker’s Vegetarian Cooking
    url   = /usedbooks/cookbooks.htm#Betty Crocker’s Vegetarian Cooking
    title = Betty Crocker’s Vegetarian Cooking
    caption = Betty Crocker’s Vegetarian Cooking

    */

      # file containg your image descriptions

      $IMG_CONFIG_FILE = ‘images.ini’;

      # You shouldn’t need to change anything below this point

      function showImage( $ini=null ) {
    global $IMG_CONFIG_FILE;
    # if no custom ini file has been specified, use the default
    $ini_file = $ini ? $ini : $IMG_CONFIG_FILE;
    # read the config file into an array or die trying
    $images = @parse_ini_file($ini_file,true);
    if (! $images) {
    die('Unable to read ini file.');
    }
      # pick a random image from the parsed config file
      $img = array_rand($images);
      # get the selected image’s css id if one exists
      $id = $images[$img][‘id’] ?
        sprintf( ’ id=”%s” ‘, $images[$img][‘id’] ) :
        ‘’;
      # get the selected image’s css class if one exists
      $class = $images[$img][‘class’] ?
        sprintf( ’ class=”%s” ‘, $images[$img][‘class’] ) :
        ‘’;
      # get selected image’s dimensions
      $size = @getimagesize( $images[$img][‘src’] );
      # if an url was specified, output the opening A HREF tag
      if ( $images[$img][‘url’] ) {
    printf(
    '’,
          $images[$img][‘url’],
          $images[$img][‘title’]
        );
      }
      # output the IMG tag
      printf(
        ‘%s’,
        $images[$img][‘src’],
        $images[$img][‘alt’],
        $size[3],
        $id,
        $class
      );
      # if an url was specified, output the closing A HREF tag
      if ( $images[$img][‘url’] ) {
    echo('
    ');
    }
      <pre>$caption = $images$img;<pre>
      if ($caption)  {
    printf('%s.', $caption);
    }

      }

     

    ?>

    Copy & paste the code below to embed this comment.
  39. I have the script running perfectly on my own laptop testing server, but when i uploaded the pages to the web, I get the above error.

    can anyone shed any light on the subject.

    the include statement is in the correct place at the top of the page, and as i said works fine on my laptop.

    thanks for all your help in advance.

    Russ

    Copy & paste the code below to embed this comment.
  40. I had a reaaly good ranking in Google - until i rolled this out on my entire site. I have consitently held a rank of between number 1 to number 3 of the search results for atleast the last 6 months. If search rankings bother you, consider something else.

    Those having troubles with it finding the INI file… I had to enter my full server path to the ini file. If you dont know this your host will be able to tell you.

    Copy & paste the code below to embed this comment.
  41. I can’t get the captions to work… this is what I put in the .php file:

    # add caption
    $caption = $images[$img];
    if ($caption) {
    printf('', $caption); }

    and added “caption = ” to the .ini file. Can someone help? Thx.

    Copy & paste the code below to embed this comment.
  42. Hi great code works perfect is there a way to out the images at a certain size.

    thanks

    Copy & paste the code below to embed this comment.
  43. I have your script working on my site and am wondering if there is a way to give each image a click-through address?

    I am asking because the images that I want to add to our site are ads.

    Thanks for the great script!  This is my first excursion into PHP and it worked perfectly without any hitches at all.

    Copy & paste the code below to embed this comment.
  44. I liked the simplicity of rotate—except that since it had an image
    path that was fixed, the browser would cache that image.  So if I used
    it as part of my sidebar menu system then it would not change the image
    as I moved from page to page, unless I explicitly reloaded the page.

    I like rotator’s actually writing the link in, and the various features for adding information to it.

    In the long run I can see the possiblity of a suite of similar tools.

    With rotator you must hand edit a file for the directory.  It would make
    better sense to write a file per image. Give the text file the same name as the image file, but a different extension. In this way, the manipulation
    of the file and the image can be done together, without messy edits.

    This also allows for the non-existence of the extra information.

    I’m brand new to php, but I’ve done some perl before.  I’m going to try
    to make a minimalist rotate that writes image links, using bits from
    both programs.

    Thanks for the two progs.

    ***

    Much of the time there is no need to specify the size of the image. That can be offloaded to the browser.  Use your css to specify how big
    you want the image to be in the containing div.

    Copy & paste the code below to embed this comment.
  45. I’ve found this code quite useful, but I too am trying to figure out where to insert a TARGET=_BLANK so as to open links in a new window.

    also of secondary interest would be to add a Border=0 feature

    Copy & paste the code below to embed this comment.
  46. Does anyone get the error messages on lines 44 & 48 of the rotator.php file saying undefined index. I don’t need the id for styling, but have created the id in the style sheet to see if it goes away and it doesn’t. It doesn’t affect the rotator when uploaded but I hate error messages on my testing server. Any help??

    Copy & paste the code below to embed this comment.
  47. In order to get only unique images add the following to rotator.php script above the ‘get the selected image’s css id if one exists’ statements (note: you only require one ‘pick a random image from the parsed config file’ statement):

    //initialise array if it has not been created
    if ( empty($_SESSION) ) {
    $_SESSION['selected_pics_array'] = array("0");
    }

    // pick a random image from the parsed config file
    $img = array_rand($images);

    //ensures that the image is unique
    while(in_array($img,$_SESSION[‘selected_pics_array’])) {
    $img = array_rand($images);
    }

    //add the image to the list of displayed images
    array_push($_SESSION[‘selected_pics_array’], $img);

    Also put the following above your <html> tag:
    <?php session_start(); ?>

    After adding all of your “<?php showImage(); ?>” statements add the following:
    <?php
    session_destroy();
    ?>

    Copy & paste the code below to embed this comment.
  48. I am new to PHP but the script is working fine for what I want…only how do I get rid of the border that is automatically being added to my photos? I dont want a border at all…

    Thanks

    Copy & paste the code below to embed this comment.
  49. Going way back to #4’s request. Can this script be adapted to display only text links instead of banners. I haven’t had any success isolating the ‘caption’ part and suppressing the image

    Copy & paste the code below to embed this comment.
  50. This was a great script but how do you set it up on a timer?

    Copy & paste the code below to embed this comment.
  51. I had this same problem when I switched local server. Everything had previously worked fine.

    I found that if you created both an ID & Class in the stylesheet and left the ID blank (but the Class specified) it corrected the error.

    Copy & paste the code below to embed this comment.
  52. Javascript, with a timing loop and a lot of images addressed by their array numbers, is fine for slower rotating where the images will certainly have time to load, or where you’ve done preloading. But experiments have shown me that if you don’t want people waiting for preloads, or if the image size is quite large, or, especially, if the rotation speed is fast (under a half second), there’s a better way. Having tried a rotator doing 600x400 images at 1/3 second, with and without preloads, I found that the browsers went nuts. I use smaller images and 1 second times and standard Javascript image cycling with no problem, but the browsers draw the line at the bigger/faster combo. So guess what works perfect on all browsers? I made an 800K gif with lots of images in it and 1/3 second timing, using ImageForge and MS Gif Animator. I expected browsers to choke on this since no preloading occurred. But they all smiled, winked, and purred! I hope this helps anyone who’s tried to make rotators work with big, fast images and tore his hair out at the results.

    Copy & paste the code below to embed this comment.
  53. i found how to ad the target option:
    in the rotator.php, write like that:

      # if an url was specified, output the opening A HREF tag
      if ( $images[$img][‘url’] ) {
        printf(
          ‘<a href=”%s” title=”%s” target=”%s”>’,
          $images[$img][‘url’],
          $images[$img][‘title’],
          $images[$img][‘target’]
        );
      }


    in the .ini file ad a line:
    target =_blank

    greats!!

    Copy & paste the code below to embed this comment.
  54. If you are trying to get rid of the border it is probably not an image border but a ‘link border’. This worked on my page:-
    A:hover{color: red; text-decoration:none}
    a img {border:0;]
    The first line just sets the hover attributes the second the link border.
    Secondly you can set the script up to reference different ini files on the same page allowing different pools of images on different places on the same page by renaming the function ‘showImage’to showImage1, showImage2,showImage3 etc and saving each changed file as a separate rotator.php file. ie rotator1.php, rotator2.php etc. The image is called by <?php showImage(‘images2.ini’); ?> or
    <?php showImage(‘images3.ini’); ?>
    This might be inelegant but it works . . .

    Copy & paste the code below to embed this comment.
  55. Excellent information here cause in my language, there are not much good source like this. This interesting post made me smile. Maybe if you throw in a couple of pictures it will make the whole thing more interesting.

    Copy & paste the code below to embed this comment.
  56. Can you add a tooltip to each image?

    Copy & paste the code below to embed this comment.
  57. I just wanted to say thanks!  I extended your script to provide a random image for the side and then I grabbed all the images and randomly selected their order to make a group of thumbnails.  Perfect for a new webcomic facebook app I made. 

    Thanks again!

    Copy & paste the code below to embed this comment.
  58. I like this site! It’s full of educational information which are related to my works. I am glad to find it. Wink! GAR Labs

    Copy & paste the code below to embed this comment.
  59. I like this site! It’s full of educational information which are related to my works. I am glad to find it. Wink! GAR Labs

    Copy & paste the code below to embed this comment.
  60. Thank you for great article and code.  Just found this when getting frustrated with trying to use a JS to rotate images.  Now my client - my wife! - wants the images to rotate every x seconds as opposed to reload.  I know I can do it in JS, but can this be done in php, is there an easy fix/addition to this code to do it?  Any suggestions appreciated.

    Copy & paste the code below to embed this comment.
  61. Sorry, commenting is closed on this article.