Randomizing your background every refresh!

Discussion in 'PHP' started by lasti, Jul 21, 2011.

  1. #1
    I got this code & it works pretty well.

    <?php
    
    	$folder = '.';
    
        $extList = array();
    	$extList['gif'] = 'image/gif';
    	$extList['jpg'] = 'image/jpeg';
    	$extList['jpeg'] = 'image/jpeg';
    	$extList['png'] = 'image/png';
    	
    
    // You don't need to edit anything after this point.
    
    
    // --------------------- END CONFIGURATION -----------------------
    
    $img = null;
    
    if (substr($folder,-1) != '/') {
    	$folder = $folder.'/';
    }
    
    if (isset($_GET['img'])) {
    	$imageInfo = pathinfo($_GET['img']);
    	if (
    	    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $folder.$imageInfo['basename'] )
        ) {
    		$img = $folder.$imageInfo['basename'];
    	}
    } else {
    	$fileList = array();
    	$handle = opendir($folder);
    	while ( false !== ( $file = readdir($handle) ) ) {
    		$file_info = pathinfo($file);
    		if (
    		    isset( $extList[ strtolower( $file_info['extension'] ) ] )
    		) {
    			$fileList[] = $file;
    		}
    	}
    	closedir($handle);
    
    	if (count($fileList) > 0) {
    		$imageNumber = time() % count($fileList);
    		$img = $folder.$fileList[$imageNumber];
    	}
    }
    
    if ($img!=null) {
    	$imageInfo = pathinfo($img);
    	$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    	header ($contentType);
    	readfile($img);
    } else {
    	if ( function_exists('imagecreate') ) {
    		header ("Content-type: image/png");
    		$im = @imagecreate (100, 100)
    		    or die ("Cannot initialize new GD image stream");
    		$background_color = imagecolorallocate ($im, 255, 255, 255);
    		$text_color = imagecolorallocate ($im, 0,0,0);
    		imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
    		imagepng ($im);
    		imagedestroy($im);
    	}
    }
    
    ?>
    Code (markup):

    The solution to send headers with the randomized image seems to cause load issues.. Can any one provide me with a clean up code for this?/ Which makes the load time faster.
     
    lasti, Jul 21, 2011 IP
  2. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    name the image files in sequence like image1.jpg, image2.jpg and so on.
    Use mt_rand() function to set the appropriate image for src attribute or style sheet code.
    Like
    <img src="imgage<?php echo mt_rand(1,5); ?>.jpg" />
    PHP:
    If your image file names are having specific names and different extensions probably you can store the image file names in and array and do the same thing as described above:
    $arr[1]="someimage.jpg";
    $arr[2]="anotherone.png";
    ...
    <img src="<?php echo $arr[mt_rand(1,5)]; ?>" />
    PHP:
     
    Last edited: Jul 21, 2011
    techbongo, Jul 21, 2011 IP
  3. lasti

    lasti Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thank you, but your code throws some error.

    PHP Parse error:  syntax error, unexpected '<'
    Code (markup):
    I'm using this code on vbullletin.
     
    lasti, Jul 22, 2011 IP
  4. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #4
    <?php
    $images=array('img1','img2','img3','img4','img5');
    $rand=mt_rand(0,4);
    echo "<style>body{background-image:url('".$images[$rand]."')}</style>";
    ?>

    DONE for background images upto 5. SIMPLE AINT IT? :)
     
    spaceman12, Jul 22, 2011 IP
  5. gimles

    gimles Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :) that's the way to do it right there, very elegant spaceman12 :)
     
    gimles, Jul 22, 2011 IP
  6. lasti

    lasti Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Still no luck guys :( Also mention the path if it needs changing.
     
    lasti, Jul 24, 2011 IP
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    did you change the img1, img2, img3, and so forth to the full web page address and name of each of the images? Post your entire php/html code for us to edit it for you.
     
    exodus, Jul 24, 2011 IP
  8. lasti

    lasti Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #8
    yes i changed it to image1.jpg or should i change it to the image url?
     
    lasti, Jul 24, 2011 IP
  9. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #9
    sure try that. Without seeing your full source I can't tell you much else to do.
     
    exodus, Jul 24, 2011 IP
  10. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #10
    
    <?php
    $images=array('img1','img2','img3','img4','img5');
    $rand=mt_rand(0,4);
    echo "<style>body{background-image:url('".$images[$rand]."')}</style>";
    ?>
    
    Code (markup):
    Change img1,img2 etc to the full path of the images being hosted and all should be done and working! Your original code is just a mess of lumps means to degenerate resources and nothing more.
    Now don't act like an absolute noobs :D
     
    spaceman12, Jul 25, 2011 IP
  11. lasti

    lasti Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #11
    Sorry, Your code is not seem to be working.
     
    lasti, Jul 25, 2011 IP
  12. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Code given by spaceman12 is absolutely correct. Try to learn PHP before starting over. Otherwise, you won't know why copy-pasted code snippets are not working properly.
     
    techbongo, Jul 26, 2011 IP