Jump to content

Recommended Posts

Posted

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

Posted

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);


?>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...