1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Image rotation

Discussion in 'PHP' started by buckmajor, Dec 20, 2009.

  1. #1
    Hi there

    What is the most convenient php script for a background image rotation?

    I want to be able to have my background image (800px X 600px) to rotate with different images on every page load or refreshing a page?

    Example:
    http://www.benharper.com/

    Would anyone know how to accomplish this?

    Many thanks in advance
    CHEERS :)
     
    buckmajor, Dec 20, 2009 IP
  2. ibg

    ibg Member

    Messages:
    25
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Well something like this should do the trick :
    <?php 
    $image[1]='http://www.yoursite/image.jpg';
    $image[2]='http://www.yoursite/image2.jpg';
    $image[3]='http://www.yoursite/image3.jpg';
    $image[4]='http://www.yoursite/image4.jpg';
    $image[5]='http://www.yoursite/image5.jpg';
    $image[6]='http://www.yoursite/image6.jpg';
    $randnum=rand(1,6);
    echo '<img scr="'.$image[$randnum].'">';
    ?>
    PHP:
     
    ibg, Dec 20, 2009 IP
  3. NodLemon

    NodLemon Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $imageList = array("image1.png", "image2.png", "image3.png");
    echo $imageList[rand(0, 2)];
    PHP:
     
    NodLemon, Dec 20, 2009 IP
  4. jedi.knight

    jedi.knight Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How about this?

    
    $max = 5;
    echo 'image' . rand(1, $max) . '.png';
    
    Code (markup):
     
    jedi.knight, Dec 20, 2009 IP
  5. shuman202

    shuman202 Well-Known Member

    Messages:
    638
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    145
    Digital Goods:
    1
    #5
    thanks guys usefully codes
     
    shuman202, Dec 20, 2009 IP
  6. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Here, I wrote a script for it.

    http://freebies.enspherant.com/rotating-image/

    Actually, it tuens out the since all of the browsers I tried allow you to show an image with a .php extension and the proper content-type headers it was fairly easy to make a script that will rotate gif/jpg/png images automatically in any css style or html IMG tag.

    Turns out it's useful for more than just rotating backgrounds, you could rotate banners with it, too. The linked page shows a demo, you could even have several different random backgrounds in use on the same page.
     
    goliath, Dec 20, 2009 IP
  7. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #7
    WOW! Thanks guys. I will give them a go, and report back on my experience.

    CHEERS :)
     
    buckmajor, Dec 20, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Put this at the top of your page
    <?php
    $dir = 'path/to/images/';
    $imgs = glob($dir.'*.jpg');
    $img = '<img src="/'.$imgs[array_rand($imgs)].'" />';
    ?>
    PHP:
    Then wherever you want the image just put
    <?php echo $img; ?>
    PHP:
    This script will randomly put an image with the .jpg extension from the directory path/to/images/ into an <img> tag you can use with just echo $img
     
    JAY6390, Dec 20, 2009 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No, the script works by getting a list of images directly from a folder and outputting them
     
    JAY6390, Dec 21, 2009 IP
  10. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    <?php
    $dir = 'bg/';
    $imgs = glob($dir.'*.jpg');
    $img = '<img src="/'.$imgs[array_rand($imgs)].'" />';
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Page1</title>
    <style type="text/css">
    <!--
    body {
    	background-image: url(<?php echo $img; ?>); // I tried url(bg/<?php echo $img; ?>); too
    	margin-top: 20px;
    }
    
    Code (markup):
    I did this but the background image doesn't appear.
    Am I doing something wrong? Thanks
     
    cat_fich, Dec 21, 2009 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Change this line
    $img = '<img src="/'.$imgs[array_rand($imgs)].'" />';
    to
    $img = $imgs[array_rand($imgs)];
    that should solve it
     
    JAY6390, Dec 21, 2009 IP
  12. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It's working now. Thanks
     
    cat_fich, Dec 21, 2009 IP
  13. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #13
    No problem :)
     
    JAY6390, Dec 21, 2009 IP
  14. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    <?php
    
    /**********************
    * Image Rotater
    ***********************
    * Written by Joel Larson (Coded Caffeine)
    * http://thejoellarson.com/
    *
    * Allows a person to add images with descriptions
    *   and pick an image randomly from a list.
    **********************/
    # Defined variables from paranoia.
    $images = array();
    $output = '';
    /*********************/
    
    # Copy/paste as many as you need.
    # There are two shown for an example! (Make sure they are unique!)
    $images[] = array(
    	'path' => 'URL',
    	'alternate' => 'ALTERNATE TEXT',
    	'title' => 'DESCRIPTION'
    );
    
    $images[] = array(
    	'path' => 'URL',
    	'alternate' => 'ALTERNATE TEXT',
    	'title' => 'DESCRIPTION'
    );
    
    # Set the int for total images in the first array level.
    $total_images = count($images);
    
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, $total_images);
    
    /**
    * Set the output that's being generated.
    */
    
    # If a path is defined..
    if( ! empty($images[$id]['path']))
    {
    	# Create the image with path.
    	$output =  '<img src="'.$images[$id]['path'].'" ';
    	
    	# If the alt text is defined, add it.
    	if( ! empty($images[$id]['alternate']))
    		$output .= 'alt="'.$images[$id]['alternate'].'" ';
    	
    	# If the title text is defined, add it.
    	if( ! empty($images[$id]['title']))
    		$output .= 'title="" ';
    
    	$output .= '/>';
    }
    
    # Echo the result.
    echo $output;
    PHP:
     
    CodedCaffeine, Dec 21, 2009 IP
  15. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #15
    Sweet! I tested all of the PHP codes, but couldn't get all of them to work properly for some reasons except for this code:
    <?php
    
    /**********************
    * Image Rotater
    ***********************
    * Written by Joel Larson (Coded Caffeine)
    * http://thejoellarson.com/
    *
    * Allows a person to add images with descriptions
    *   and pick an image randomly from a list.
    **********************/
    # Defined variables from paranoia.
    $images = array();
    $output = '';
    /*********************/
    
    # Copy/paste as many as you need.
    # There are two shown for an example! (Make sure they are unique!)
    $images[] = array(
        'path' => 'URL',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'DESCRIPTION'
    );
    
    $images[] = array(
        'path' => 'URL',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'DESCRIPTION'
    );
    
    # Set the int for total images in the first array level.
    $total_images = count($images);
    
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, $total_images);
    
    /**
    * Set the output that's being generated.
    */
    
    # If a path is defined..
    if( ! empty($images[$id]['path']))
    {
        # Create the image with path.
        $output =  '<img src="'.$images[$id]['path'].'" ';
       
        # If the alt text is defined, add it.
        if( ! empty($images[$id]['alternate']))
            $output .= 'alt="'.$images[$id]['alternate'].'" ';
       
        # If the title text is defined, add it.
        if( ! empty($images[$id]['title']))
            $output .= 'title="" ';
    
        $output .= '/>';
    }
    
    # Echo the result.
    echo $output;
    
    ?>
    PHP:
    By the way, I forgot to mention that I was using XAMMP to test my PHP pages. Would that have any effect on the other PHP codes?

    I was thinking instead of typing out the URL path individually, is there a PHP code where can link a 'dir' folder with images and have them rotate randomly? If not, then I guess I can type out the path individually.

    Thanks heaps guys.
    CHEERS :)
     
    buckmajor, Dec 22, 2009 IP
  16. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #16
    The code I gave should work although might not on a windows installation of XAMPP due to the paths. Changing that should do it (note that it is for jpg files only)
     
    JAY6390, Dec 22, 2009 IP
  17. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #17
    My bad, I didn't know it randomly does the folder, all you have to do is put the url name of that the image in the PHP code.
    $images[] = array(
        'path' => 'URL',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'DESCRIPTION'
    );
    PHP:
     
    buckmajor, Dec 22, 2009 IP
  18. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #18
    Cool! I had a funny feeling that it might be the windows installation of XAMPP. It took a while to realize that it didn't work because of XAMPP. Oh well, at least I know now. Yep all my files are .jpg.
     
    buckmajor, Dec 22, 2009 IP
  19. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #19
    It shouldn't have much of an effect. The images just need to be relative to the PHP file.
     
    CodedCaffeine, Dec 23, 2009 IP
  20. jedi.knight

    jedi.knight Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    The following script uses the filename as the description. For example, "Golden_Gate_Bridge.jpg" will set the value of $image_caption to "Golden Gate Bridge".

    This way, all you have to do is to simply copy (and later delete if necessary) files to and from the images directory. You can change the title by simply changing the filename.

    
    <?php
    
    /***********************************************************************
     *
     * Pick randndom image and display a title / alternate text based on
     * the filename.
     *
     * The filename can have spaces, and should have a jpg, png or gif
     * extension.  Underscores also will be turend into spaces.
     *
     * Written by the Jedi Knight
     *
     * http://forums.digitalpoint.com/member.php?u=371818
     *
     **********************************************************************/
    
    $image_dir = 'images';
    
    /* No need to change anything below */
    
    $images = glob("$image_dir/*.jpg");
    $images = array_merge($images, glob("$image_dir/*.png"));
    $images = array_merge($images, glob("$image_dir/*.gif"));
    $image = $images[array_rand($images)];
    $image_caption = str_replace('_', ' ', preg_replace('/^.*[\/\\\\]([^\/\\\\]+)\.[^\/\\\\]+$/', '$1', $image));
    
    /* Now you can use $image and $image_caption as necessary */
    
    ?>
    
    PHP:
    For example, to use the random image as the body background, use the following, as someone else has suggested before:
    
    <style type="text/css">
    <!--
    body {
    	background-image: url(<?php echo $image; ?>);
    	margin-top: 20px;
    }
    -->
    </style>
    
    PHP:
    To show a description, simply print the value of $image_caption.
     
    jedi.knight, Dec 23, 2009 IP