?
Solved

Display Random Images

Posted on 2006-07-23
7
Medium Priority
?
294 Views
Last Modified: 2006-11-18
This script will display 1 random image from a directory... I would like to make it so grabs 5 different from the directory and displays them.... if I could make the alt tags match the name of the image that would be a very cool added bonus.. any thoughts??

<?php
$dir=opendir("/httpdocs/wp-content/themes/stormer/random");
//This is the directory route to the folder
$directory="stormer/random/";
//This is a relative link to the directory if it is not in the same directory as the file you are displaying the images on

$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";
if(!$dir)
{
die("Failed to read directory");
}
$s=readdir($dir);
$count="0";
$image;
while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);

//Spit it out
$limit=count($image);
$limit--;
$randNum=rand(0,$limit);
$size=getimagesize("$directory$image[$randNum]");
echo "<img alt='' src=\"$directory$image[$randNum]\" $size[3] />";
?>
0
Comment
Question by:cstormer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17165505
what about this:

//Spit it out
$limit=count($image);
array_shuffle($image); // to randomly organize the array items:

$spit_count = 0;
while ( $spit_count <5 AND $spit_count < $limit )
{
   echo "<img alt=\"{$image[$spit_count]}\" src=\"{$directory}{$image[$spit_count]}\" {$size[3]} />";  
   $spit_count ++;
}

0
 

Author Comment

by:cstormer
ID: 17167118
That seems like a really good idea.. but nothing happened when I added it... hmm.. I'm not really a php expert.. I'm trying to figure it out.. I think this would work.. but there seems like their might be a problem with the code?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17167138
of course, you have to remove what you had after
//Spit it out

and put this instead (corrected 2 small things)

//Spit it out
$limit=count($image);
array_shuffle($image); // to randomly organize the array items:

$spit_count = 0;
while ( $spit_count <5 AND $spit_count < $limit )
{
   $size=getimagesize("$directory{$image[ $spit_count]}");
   echo "<img alt=\"{$image[$spit_count]}\" src=\"{$directory}{$image[$spit_count]}\" {$size[3]} />";  
   $spit_count ++;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cstormer
ID: 17167187
yes... i did remove and substituted straight up...

the page seems to stop at the point where the script is running.  Now I know the first version worked for one image.. so the directories and things are ok.... is there a way to "debug" or see what line is causing  it to stop?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17167209
you should enable error logging and error display... I guess I have some typo (I don't have a php website here to test myself)
0
 

Author Comment

by:cstormer
ID: 17167214
I'm not sure if this changes the code... but the exact directory paths..

$dir=opendir("/var/www/vhosts/domain.com/httpdocs/wp-content/themes/stormer/random");
//This is the directory route to the folder
$directory="http://domain.com/wp-content/themes/stormer/random/";
//This is a relative link to the directory if it is not in the same directory as the file you are displaying the images on
0
 

Author Comment

by:cstormer
ID: 17167275
Eitherway you got me off to a great start thanks! :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question