473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Choose random image from dir and display it

I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP
Jul 17 '05 #1
4 3406
You probably find it safer to keep doing it the way you are.

Or instead modify it to build an array containing filenames and then
randomly pick an entry from the array. This is to account for if a file
is ever removed, no errors would occur.
kingofkolt wrote:
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP

Jul 17 '05 #2
neur0maniak <us****@neur0ma niak.co.uk> wrote in news:41830267$0 $43631
$e*******@ptn-nntp-reader04.plus.n et:
You probably find it safer to keep doing it the way you are.

Or instead modify it to build an array containing filenames and then
randomly pick an entry from the array. This is to account for if a file
is ever removed, no errors would occur.


Try this, as described above. Forget where I found the code but it works
just fine. $dirpath should be a fullpath to the appropriate directory and
requires a slash at the end... or you can add it to the couple of lines
in between the path and the image filename. Everything in the directory
except those in the if statement will be put into the array.

$dir_contents = array();
$d = dir($dirpath);
while ( $crnt_file = $d->read() ) {
if ($crnt_file == "." || $crnt_file == ".."
|| is_dir($dirpath .crnt_file))
continue;
$dir_contents[] = $dirpath.$crnt_ file;
}
$d->close();
Jul 17 '05 #3
Fox
kingofkolt wrote:
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP


php 4.3 or greater:

$imgGroup = glob("images/random/{*.jpg,*.gif,*. png}", GLOB_BRACE);

echo "
<img src = {$imgGroup[rand(0, count($imgGroup )-1)]} />
";

Fox
**************

Jul 17 '05 #4
"kingofkolt " <je**********@c omcast.net> wrote in message news:<WMBgd.340 547$D%.310394@a ttbi_s51>...
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP


Yes. You can do all that in a couple lines:

$file = array_rand(glob ("images/random/*.gif"));
echo "<img src=\"$file\">" ;
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2081
by: lucy | last post by:
Hello comp.lang.js.I need a script to rotate a background image on each newpage load, given the id of the element and a list (array?)of 2 or more image paths ("img1.jpg", "img2.jpg", ...).Actually, depending on how I write the HTML, I guess I'dneed the script to set style.backgroundImage or src.I'd need to run it on a few elements, something...
4
2739
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is...
7
2337
by: Spartanicus | last post by:
Afaik the use of a <noscriptelement is frowned upon nowadays. For a JS random image changer I tried to use a replacement by having the script change the HTML src attribute value of an img element. The trouble is that in some situations like over slow connections (on initial load when the JS is in an external file), or with a slow client the...
4
3066
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random...
0
1520
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random...
1
1763
by: KoRnDragon | last post by:
I need a Javascript function that will display X random images on a page that also doesn't display the same image twice. Bonus points if it grabs the max array from the folder and sets it. Even more points if it grabs all the image names from the folder. Thanks in advance!
1
2644
by: Sahus Pilwal | last post by:
Hi, I hope someone can help me with this. I'm new to .NET and in fact server side programming and have a small query I'm sure... I'm using the System.IO Namespace with a For - each and If then statement to randomly generate images to display in a <asp:image/image control. The random images are selected from a fixed folder on the...
8
2429
by: Kentor | last post by:
Hello, I have users that submit images to my website. By default I choose to make the width bigger than the height because usually pictures are taken horizontally... But in some cases people take pictures vertically so what I'm getting at is... can I know whether an image was taken horizontally or vertically so I can make the proper width/...
10
3394
by: lifity | last post by:
hi, i am stuck on part of my project and very new to VB. i had cut my picture into 6 equals part but need to display them randomly, without repeating the same part of the pic again, in random sequence. eg, pic1,pic2,pic3,pic4,pic5,pic6 to ==> pic3,pic2,pic6,pic1,pic5,pic4 below are part of my code Dim theImage(0 To 7) As Object ...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.