djmike Posted September 3, 2006 Share Posted September 3, 2006 I am looking for a PHP script that will randomly display an image from a folder of around 20 images, although there will be more soon. The images names are all initials, e.g. TY. I could do a script to pick images if their names were all number going up from 1, but I'm not sure if this is possible with the above?Thanks for any suggestions Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/ Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 How about using the glob() function to generate an array of the images and then using rand() to select an image from the array? Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85172 Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Ok, could you give example code please. I understand the rand function, and have already got a function using the time() to get a random number. I just don't understand who I'd use glob() with the images to create an array?Thanks Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85188 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 [code]<?php// in the folder with the imagesforeach (glob("*.jpg") as $filename) { $image[$i] = $filename; $i++;}$rand_image = rand(0,count($image)-1);$tmp = $image[$rand_image];$size = getimagesize($tmp);echo "<img src='". $tmp. "' ". $size[3]. " alt='". $tmp. "'>";?> [/code] Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85196 Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks, that works fantastically.I just need to now change it so that upon clicking the picture (using <a href) the user is directed to a php page with ?t= followed by the initial that were the picture's name. So for the image TY.jpg, the link would be mysite.com/viewer.php?t=TYHowever, as I created an array, I only have the complete paths stored not the original files. Is the best way to create another array for the file names, ut I'm not sure how this would work it may randomise twice so the link doesn't match the imageThanks again Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85238 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 [code]<?php// in the folder with the imagesforeach (glob("*.jpg") as $filename) { $image[$i] = $filename; $i++;}$rand_image = rand(0,count($image)-1);$tmp = $image[$rand_image];$size = getimagesize($tmp);$url_path = "../relative/path/to/folder/where"; // where viewer.php exists - adjust to suit.// what's before the . in the image name?$bits = explode(".",$tmp); // $bits[0] will be TY, etc.$url = $url_path. "/viewer.php?t=". $bits[0];echo "<a href='". $url. "'><img src='". $tmp. "' ". $size[3]. " alt='". $tmp. "' border='0'/></a>";?>[/code]That should help you out as long as you have the right relative path from the images folder to viewer.php. You can always view-source on the generated image page to see what URL it's pointing to Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85250 Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks so much for the help, I got it sorted, but bits[0] display the whole file path and so didn't work. I had to explode it further to remove the file path:[code]$bits = explode(".",$tmp) ; // $bits[0] will be filepath/TY, etc.$bits2 = explode("path/",$bits[0]) ; // $bits2[1] will be TY, etc.[/code]Thanks again Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85280 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 You're welcome. Glad it all worked out. Link to comment https://forums.phpfreaks.com/topic/19578-randomly-display-any-image-from-a-set-folder/#findComment-85283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.