gratty Posted April 15, 2009 Share Posted April 15, 2009 I'm pretty new to PHP so bear with me , I've been looking for a PHP script that I can use to randomly display images. However, I have a large amount of directories (over 100) that contain these images, and I need to be able to pass parameters to the PHP script based on the content, to ensure that I get images from the correct directory. For example, if I'm posting content about tigers and I want to post a tiger picture, then I'd point the script to images/animals/tigers. I've found a lot of scripts that can display images from a single directory, but none that provide the sort of flexability that I need. Additionally, I need the script to be able to be called from outside of an HTML document, so if I needed to call an image, I could do something like the following: <img src=(Random.php getRandomImage($directory)) alt = "Random Image" /> Is this possible at all? I found a script that could do this before, but it had a header() return, and I need to display six images, while the header only is able to return one. Would it be possible to adjust this following code to meet these requirements? <?php /* By Matt Mullenweg > http://photomatt.net Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php Latest version always at: http://photomatt.net/scripts/randomimage */// Make this the relative path to the images, like "../img" or "random/images/". // If the images are in the same directory, leave it blank. $folder = ''; // Space seperated list of extensions, you probably won't have to change this. $exts = 'jpg jpeg png gif'; $files = array(); $i = -1; // Initialize some variables if ('' == $folder) $folder = './'; $handle = opendir($folder); $exts = explode(' ', $exts); while (false !== ($file = readdir($handle))) { foreach($exts as $ext) { // for each extension check the extension if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive $files[] = $file; // it’s good ++$i; } } } closedir($handle); // We’re not using it anymore mt_srand((double)microtime()*1000000); // seed for PHP < 4.2 $rand = mt_rand(0, $i); // $i was incremented as we went along header('Location: '.$folder.$files[$rand]); // Voila! ?> Could anyone point me in the right direction? Link to comment https://forums.phpfreaks.com/topic/154241-random-image-script-and-passing-parameters-in-html/ Share on other sites More sharing options...
schilly Posted April 15, 2009 Share Posted April 15, 2009 well for one, if you need to display 6 images you won't be able to do: <img src=(Random.php getRandomImage($directory)) alt = "Random Image" /> i would suggest making an image creation function that takes in your directories: <?php function createImage($dir,$subdir){ // create the image from a random image from the provided directory structure // set your headers to tell the browser an image is coming then output the image data } createImage($_GET['dir'],$_GET['subdir']); ?> then call it by doing <img src='createImage.php?dir=animals&subdir=tigers'> something like that should work. if you want 6 images i would grab 6 images randomly then format it in whatever html you need then output that. you wont be able to use it as an image source though. Link to comment https://forums.phpfreaks.com/topic/154241-random-image-script-and-passing-parameters-in-html/#findComment-810989 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.