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.
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:
Thank you, but your code throws some error. PHP Parse error: syntax error, unexpected '<' Code (markup): I'm using this code on vbullletin.
<?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?
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.
<?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
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.