Jump to content


Photo

Advanced Randomized Image Display Script/Automated to 100 Images


  • Please log in to reply
5 replies to this topic

#1 ridiculous

ridiculous
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 28 October 2006 - 10:41 PM

So I've been looking around the net trying to find a script that would randomly display MANY images from a file on one's server. MANY in this sense would too many to itemize each image in an array.

I have an Actionscript file that does this just peachy for the flash version of my site, but I'd like to use a PHP script for the non-flash version. Any ideas?


P.S.

I am aware of some scipts already made available here, maybe there is a way to fix them so that a 50+ images can be drawn from a server directory without being itemized:

i.e.


-------------------------------------

http://www.chipmunk-.../page.php?ID=23
Chipmunk Imagerotator
This is all the code for image rotator:

"; } else if($imagevar==2) { print ""; } else if($imagevar==3) { print ""; } else if($imagevar==4) { print ""; } ?>

The rand() function in php, generates a random number, the first passed value is the lower bound, the second is the higher bound. Just paste this code where-ever you want the rotated image and replace the 'URL of image #' with the URL of the image. To adjust the number of images rotated, simply change the $imagevar=rand(1, 4) to $imagevar=rand(1, # of images you want to rotate here) and then for each additional image create an else if case. For example for the fifth image you would want:

else if($imagevar==5)
{
print "";
}

If you want less than 4 images, simply take out the else if cases for that image.

------------------------------------------------------------------


Your help makes me feel warm and fuzzy.

#2 ridiculous

ridiculous
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 28 October 2006 - 11:05 PM

So, since no one else is biting...I'll share my progress.

What I'm thinking is that I should rename all the images in my server directory to a number, (i.e. 1,2,3...n). Then I figure I'll write a script to generate a random number between the first and last numbers I have as filenames and simply inject that number into a print function that prints the image.

I still would like to tweek this so that I don't have to change the script every time I change the number of images in the directory.

#3 ridiculous

ridiculous
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 28 October 2006 - 11:06 PM

Also, I'll have to lookup an image resize script.

#4 ridiculous

ridiculous
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 29 October 2006 - 04:27 AM

Gerry Rafferty hasn't helped.

#5 rempires

rempires
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts
  • LocationIndiana (USA)

Posted 29 October 2006 - 07:19 AM

I'm not quite sure if i've got what your saying but i think you do, pretty much you want to open a directory, if it's a picture in that directory you want to display it, period, if that's what you mean look at this


// Open a known directory, and proceed to read its contents
$dir = "/home/blah/public_html/blah/images/;
$picturestring= explode("public_html",$dir);

if (is_dir($dir))
{
  if ($dh = opendir($dir))
  {
        while (($file = readdir($dh)) != false)
  {
 
$testpic= explode(".",$file);
If ($testpic[1] == "png" OR $testpic[1] == "gif" OR $testpic[1] == "jpg")
{

$piclocation = "http://www.yourwebsi....$picturestring[1].$file;


hmm that's kinda confusing, i sjut ported that off mywebsite, any questions jsut ask
btw $dir has to be full directory on your server like that one shows
$piclocation is the location of the current pic the directory is reading, so next you'd do whatver you want may
echo "<img src='$piclocation'>":  - to jsut simply display it, or you could laod it into a variable, whatever

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 October 2006 - 08:03 AM

is this what you are looking for?

<?php 
function Get_Image($path) {
   $ctr = 0;

   if ( $img_dir = @opendir($path) ) {
      while ( false !== ($img_file = readdir($img_dir)) ) {
         // add checks for other image file types here, if you like
         if ( preg_match("/(\.jpg)$/", $img_file) ) {
            $images[$ctr] = $img_file;
            $ctr++;
         } // end if
      } // end while
      closedir($img_dir);
      $image = $images[array_rand($images)];
      return $image;
   } else {
        return "invalid path";
   } // end else
} // end function

$somepath = 'path/to/images/';
$image = Get_Image($somepath);
?>

you could of course modify this to return more than 1 result by changing this:

$image = $images[array_rand($images)];
to this:
$num = 50; // or however many
for ($x = 0; $x < $num; $x++) {
   $image[] = $images[array_rand($images)];
}

and then it would return an array of image names to be used as such:

// from first example:
echo "<img src = '$somepath/$image'>"; 

// example if returning array:
echo "<img src = '$somepath/{$image[0]}'>"; // first picture
echo "<img src = '$somepath/{$image[2]}'>"; // third picture

// example of displaying all of them:
foreach ($image as $val) {
   echo "<img src = '$somepath/$val'>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users