25PSi Posted January 3, 2005 Posted January 3, 2005 Hey guys. I am looking for a script of some kind that would display a random pic from the bunch of pics on my page for me. Trick is, i need it to pick ANY picture (No spcial naming convention) from given folder AND have ability to go into foders within, looking for more pictures. Follow me? Not sure i am clear. Basicly i need it to display all and any picture (Say all .JPG) from this folder even if there are other folders with pictures inside main folder. I found some scripts but none of them seems to be able to do what i need it to. Anyone? Eugene Quote
MikeJ Posted January 3, 2005 Posted January 3, 2005 This is what I used to use. I'm not even sure where I originally picked up the example, but I'm not the original author of it, but I do know it's free to use. ><?php # Put the SERVER PATH to image directory. # INCLUDE trailing slash $dir = "/home/youraccount/public_html/images/random/"; # Put the HTML PATH to image directory. # DO NOT include trailing slash $html_dir = "http://www.******/images/random"; $dir_handle = opendir($dir); $image_array = array(); # This opens up the image directory, grabs all files with a ".jpg" extension # and puts them into an array. You can set whatever extension you'd like here. # Just switch out the .jpg for .gif, etc. If you have more than one extension, # replace this line with something like this: # if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg") || (substr($file, -4) == ".gif"))) { # This will pull up all .jpg OR .gifs. If you name all the images you want to pull # a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this: # if (($file !=".") && ($file !="..") && (substr($file, -10) == ".thumb.jpg")) { # basically you want to put the extension in quotes and then count how many characters # it is and place that after $file, - while ($file = readdir($dir_handle)) { if (($file !=".") && ($file !="..")) { $file_name = "$html_dir/$file"; $image_array[$file_name] = $file_name; } } # sorts the array. For some reason this won't work at all # unless this is done. Can anyone tell me why? sort($image_array); $count = sizeof($image_array); # trims all the URLs of whitespace and assigns each # item in the array a number to identify it. for($i = 0;$i<$count; $i++) { $image[$i] = rtrim($image_array[$i]); } # assign a random value $r = rand(0,$count-1); # get the size of a random image $size = getimagesize($image[$r]); $width = $size[0]; $height = $size[1]; # This is the HTML output. You can change any of this, but you should # leave src=\"$image[$r]\" width=\"$width\" height=\"$height\" where it is. # Remember to escape any quotation marks, etc. $img_src = "<center><img src=\"$image[$r]\" width=\"$width\" height=\"$height\" vspace=\"5\" border=\"1\" alt=\"This is a Random Image\" /></center>"; echo $img_src; # closes the directory (why keep it open?) closedir ($dir_handle); ?> Quote
25PSi Posted January 4, 2005 Author Posted January 4, 2005 Yes, good script, thanx. But it do not look like it would go into additional folders inside of main folder. Or will it? Eugene Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.