Solved

Display Random Images

Posted on 2006-07-23
7
265 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nagios 4 php error after installation 6 80
PHPStorm debugging issues 1 25
Session timeout 5 15
hosting images 4 12
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now