SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Selmer, TN
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random background image not displaying in Firefox

    Hello,

    I'm trying to set the header background for this website: http://www.rosecreekvillage.com to display a random image on page load.

    I'm using this script (from the PhotoMatt site) saved as "rotate.php" in the img/banners/panoramas/ directory:

    Code:
    <?php
    /*
    By Matt Mullenweg > http://photomatt.net
    Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
    Latest version always at:
    http://photomatt.net/scripts/randomimage
    */// Make this the relative path to the images, like "../img" or "random/images/".
    // If the images are in the same directory, leave it blank.
    $folder = '';
    
    // Space seperated list of extensions, you probably won't have to change this.
    $exts = 'jpg jpeg png gif';
    
    $files = array(); $i = -1; // Initialize some variables
    if ('' == $folder) $folder = './';
    
    $handle = opendir($folder);
    $exts = explode(' ', $exts);
    while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) { // for each extension check the extension
    if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
    $files[] = $file; // it's good
    ++$i;
    }
    }
    }
    closedir($handle); // We're not using it anymore
    mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
    $rand = mt_rand(0, $i); // $i was incremented as we went along
    
    header('Location: '.$folder.$files[$rand]); // Voila!
    ?>
    And I'm calling the script inside the CSS as:

    Code:
    #header {
    	/* Front Page Banner Image */
    	background: #222 url(/rcv/img/banners/panoramas/rotate.php) 50% 0 no-repeat;
    	border-bottom: 2px #6b8e23 solid;
    	height: 300px;
    	position: relative;
    	}
    But to no avail. This seems to work fine in IE, but not in FF. What's weird is if you go to the script directly ( http://www.rosecreekvillage.com/img/...mas/rotate.php ) it loads an image just fine.

    Does anyone have an idea of what the problem may be? Is this a bug in FF?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,154
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    background image

    Is the background image not showing at all or not changing?
    If it's not changing it probably is cached. Does reload change the image?
    If it's not loading it may need a MIME type header().
    Maybe like
    header("Content-type: image/$ext");

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Selmer, TN
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the image isn't displaying at all. In IE it displays and changes just fine. In Firefox all I get is the background color.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,154
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    dynamic background images

    Instead of
    PHP Code:
    header('Location: '.$folder.$files[$rand]); // Voila! 
    try something like
    PHP Code:
    $file $folder.$files[$rand];
    /* open the file in a binary mode */
    $fp fopen($file'rb');
    /* send the right headers */
    /* will need to work out something for different extensions */
    $ext "jpg"/* put your test ext type here */
    header("Content-Type: image/$ext");
    header("Content-Length: " filesize($file));
    fpassthru($fp);
    clearstatcache();
    exit; 
    To test it, try having only 1 type of extension. If that works, you can work out some way of associating the extension with the content-type header()

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Selmer, TN
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wonderful, that worked! Thanks so much. I'm only using .jpg's, so having only one type of extension is fine.

    Thanks so much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •