Solved

Display Random Images

Posted on 2006-07-23
7
273 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
  • 4
  • 3
7 Comments
 
LVL 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 142

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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