Forum Moderators: coopster

Message Too Old, No Replies

Using PHP for RANDOM IMAGE

php random image

         

phil4

4:19 pm on Nov 20, 2004 (gmt 0)

10+ Year Member



I searched all over to find a "non-javascript" random image loader. I found this code at phpfreaks.com (in their public code snipplets [phpfreaks.com ]), but am not a php wizbang. I do know that adding this to basic webpage, doesn't seem to work. I would imagine it is something simple, but I am at square one... I updated the 'file-dir' to match my image folder and updated the 'digit' as instructed. Any guidance or hints would be greatly appreciated! Thank you thank you thank you!

<?
// #### RANDOM PIC #########################################

$thumbstring = '';
$file_dir="pics/a10"; // DIRECTORY WITH THE PICS

$f_type="tm.jpg"; // FILE EXTENSION YOU WISH TO DISPLAY

$dir=opendir($file_dir);
while ($file=readdir($dir))
{
if ($file!= "." && $file!= "..")
{
$extension=substr($file,-6); // THIS DIGIT MUST MATCH THE NUMBER OF CHARACTERS SPECIFIED IN THE FILE EXTENSION ABOVE
if($extension == $f_type)
{
$thumbstring .= "$file¦";
}
}
}
srand((double)microtime()*1000000);
$arry_txt = explode("¦" , $thumbstring);
echo "<img src=\"".$file_dir."/".$arry_txt[rand(0, sizeof($arry_txt) -1)]."\">";

// #### END RANDOM PIC #########################################
?>

dreamcatcher

5:47 pm on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phil4,

What happens when you try the code exactly? Do you get errors? Red crosses?

To grab a random piccie from a directory, try this:

$file_dir = "images/";

$read = opendir($file_dir);

while ($file= readdir ($read))
{
if ($file!= "." && $file!= "..")
{
$images[] = $file;
}
}

closedir($file_dir);

shuffle($images);

echo "<img src=\" . $images[0] . "\" border=\"0\">\n";

Something simple like that should work. Grab the contents of the dir, put them in an array, shuffle them, then get the image in the first slot.

Hope that helps.

dc:)

phil4

6:07 pm on Nov 20, 2004 (gmt 0)

10+ Year Member



Many many many thanks, dreamcatcher, for the tip. I'll give it a whirl! To answer you question about what I would get, this is what would appear in my browser window. My first guess was something with the syntax... but php syntax is not my 2nd language, so to me it would be like finding a needle in a haystack.

"; // #### END RANDOM PIC #########################################?>

I was saving the page as .html, and switched it to .php. I am now getting a broken image, so inching closer to a solution! Unfortunately, I have to run for a bit, but will post results when I return.
Again, thanks SO much for your help. This helps me learn PHP .... we all have to start somewhere ;)

dreamcatcher

10:37 am on Nov 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem.

In PHP, as with other programming languages, you can comment out certain things. In PHP for example, the following are quoted out and would not parse:

//no display

#no display

With the following line:

"; // #### END RANDOM PIC #########################################?>

The closing tag has been quoted out, so I`m guessing thats the problem. This may have been better:

"; // #### END RANDOM PIC #################
?>

Let me know how you go with the code and if you have problems, post again. We are only too glad to help out. :)