Random Images in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ax Slinger
Forum Newbie
Posts: 1
Joined: Sat Jan 04, 2003 10:15 pm

Random Images in PHP

Post by Ax Slinger »

On HostMySig.com, an image hosting website, when you upload an image, the code they give you to link to it is a PHP link like this:

Code: Select all

http://www.hostmysig.com/data/username/image.php
They also have an option to call random images.

Code: Select all

http://www.hostmysig.com/data/username/randomsig.php
My question is, does anyone know how to do this? And if you know how, can type up the code for me? I am not a programmer, and know very little about PHP. I would like to add some random images in the header of a digital photography forum in the same way, and I think this code would do it.

Here's an example of what I would like to do, if I add the Image

Each time you hit refresh, you get a different image.

Thanks for your assistance,

Ax
NoFear
Forum Newbie
Posts: 1
Joined: Sun Jan 05, 2003 1:10 am
Location: The Dark Side of the Moon

Post by NoFear »

Try this:
******************************
<?

$pictures = array("pic1.gif", "pic2.gif", etc...);

shuffle($pictures);

echo "<img src = \".$pictures[0].\">"

?>

*********************************


This should work nice 'n dandy for ya.
=)
deadlift
Forum Newbie
Posts: 2
Joined: Fri Feb 28, 2003 3:36 pm

Post by deadlift »

actually it won't work.....
the forum will not recognize it as an image and thus it won't work
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Code: Select all

$pictures[shuffle($pictures)];
or

Code: Select all

srand(srand((float)microtime()*1000000)); 
echo $pictures[rand(0, count($pictures)-1)];
The second snippet provided by another member from a similar question posted.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

deadlift
Forum Newbie
Posts: 2
Joined: Fri Feb 28, 2003 3:36 pm

Post by deadlift »

<?
header ("Content-type: image/jpeg");
$piczahl = 0;
$pics = "";
$directory = opendir(".");
while($zaehler = readdir ($directory ))
{
if ($zaehler != "." && $zaehler != ".." && $zaehler != "pics2.php" && $zaehler != "" )
{
$piczahl++;
$pics[$piczahl] = $zaehler;

}
}
closedir ($directory);
$zufall = rand ( 1 , $piczahl);
header ("X-Debug: pics/$pics[$zufall]");
readfile ("pics/$pics[$zufall]");
?>
it'll also work this way sorry for it being german :P set dir to whatever directory you want and you don't have to rename all those pics but only jpg's work
Post Reply