Randomizing background image display.

Discussion in 'HTML & Website Design' started by Earl Sargent, Sep 23, 2013.

  1. #1
    Hey everyone, I'm having an issue with randomizing the background display through CSS and a PHP function. I've looked online and I'm trying to implement a method that uses a file called rotation.php, inside of the picture folder called rotation. I have set up the .php file inside of the image folder. What I'm having trouble doing is finding a place to insert the image source/ <img src> link. Do I put it into the html code? If so, where and how do I insert it? I will upload the .php file so you guys can see the instructions too. Thanks.
     

    Attached Files:

    Earl Sargent, Sep 23, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    IF I understand what you are trying to do -- which I'm not 100% sure I do given that train wreck of gibberish code -- it's far, FAR too complex for doing something simple. I would pull multiple glob from a image directory for the appropriate file types, array_merge them together, then use array_rand to pull a random key for the appropriate image returning it from that array. You then just echo it out using the style attribute (one of the few times I'd advocate using the STYLE attribute) on whatever element you want the background -- BODY for example:

    <?php
    
    function randImageFromDir($dir) {
    	$images = array_merge(
    		glob($dir . '/*.gif'),
    		glob($dir . '/*.jpg'),
    		glob($dir . '/*.png')
    	);
    	return $images[array_rand($images)];
    }
    
    echo '<body style="background-image:url(',randImageFromDir('backgrounds'),')">';
    
    ?>
    Code (markup):
    Far, FAR simpler that what you've been looking at. Just put whatever images you want in the directory passed to the function, and it'll pull them. No goofy manipulation of the images with GD required.
     
    deathshadow, Sep 23, 2013 IP
  3. george saviola

    george saviola Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Even simpler you can do these:

    Rename all your images into something like this :
    back1.jpg, back2.jpg, back3.jpg etc (make all the image the same format, gif, jpeg, or png)

    then find a random number with php:
    <?php
    $random=rand(1,3);
    $image="back".$random.".jpg";

    and show the background image in HTML:

    <body style="background-image:url('yourimagesfolder/<?php echo $image;?>);
     
    george saviola, Sep 24, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Simpler and more complex at the same time. Simpler/less code, harder to use as you MUST name the files according to the scheme, you MUST change the code just to change how many it selects from, etc, etc... Given the OP's original code I assumed they wanted to auto-select from whatever is in a subdirectory, not manually hard-code the choices into the site.
     
    deathshadow, Sep 24, 2013 IP
    ryan_uk likes this.