Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Display Random Images

Posted on 2006-07-23
7
Medium Priority
?
296 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

636 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