Jump to content


Photo

Randomly display any image from a set folder?


  • Please log in to reply
7 replies to this topic

#1 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 03:02 PM

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

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 03:05 PM

How about using the glob() function to generate an array of the images and then using rand() to select an image from the array?
Legend has it that reading the manual never killed anyone.
My site

#3 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 03:41 PM

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

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 03:57 PM

<?php
// in the folder with the images
foreach (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. "'>";
?>

Legend has it that reading the manual never killed anyone.
My site

#5 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 05:21 PM

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=TY

However, 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 image

Thanks again

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 05:50 PM

<?php
// in the folder with the images
foreach (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>";
?>

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
Legend has it that reading the manual never killed anyone.
My site

#7 djmike

djmike
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 September 2006 - 07:06 PM

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:
$bits = explode(".",$tmp) ; // $bits[0] will be filepath/TY, etc.
$bits2 = explode("path/",$bits[0]) ; // $bits2[1] will be TY, etc.

Thanks again

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 07:09 PM

You're welcome. Glad it all worked out.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users