Solved

Display Random Images

Posted on 2006-07-23
7
254 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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.

707 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

13 Experts available now in Live!

Get 1:1 Help Now