June 21, 200917 yr I want to generate a random image on a home page so that each time it is visited the viewer gets a different pic. I'm sure there are many ways to do this; I'm looking for a light-weight solution. Thanks in advance.
June 21, 200917 yr php random() is what you want, create a file random.php, put code to get a random #, then assign a pic to the #'s, then in the main page call the php page e.g. <?php require('random.php'); ?> google for info on the php random function
June 21, 200917 yr Plenty ways, like you said, to get this functionality. Here's a JavaScript example I Google'd for ya quickly: http://www.java-scripts.net/javascripts/Ra...ge-Script.phtml The PHP is probably better a solution due to it's ability to facilitate browsers without JS enabled, however, completely your choice. The JS example would require less investigation
June 21, 200917 yr this is what i've used in the past php: <?php $folder = 'rotate/'; $extList = array(); $extList['gif'] = 'image/gif'; $extList['jpg'] = 'image/jpeg'; $extList['jpeg'] = 'image/jpeg'; $extList['png'] = 'image/png'; $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 (943, 440) 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); } } put that in a file called rotate.php create a folder called 'rotate' and stick your images in. Then just use this <img src="rotate.php" alt="blah" /> down side is though, the alt text won't alternate, easily fixed mind, i just didn't need the alt text to rotate.
June 27, 200917 yr Author Thanks a lot guys, I'll go with PHP to support javascript-less people. Thanks again.
Create an account or sign in to comment