Random Images Script Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KiltedMile
Forum Newbie
Posts: 2
Joined: Wed Aug 06, 2003 12:44 pm

Random Images Script Help

Post by KiltedMile »

I have been tinkering with a rotating images script, and have one problem I cannot solve. In our images folder we have several images that are carbon copies, but they are named with a “._” at the beginning and we don’t want to display them with my script. For example, we want to display dumptruck.jpg but we don’t want to display ._dumptruck.jpg. Is there a way I can add an if/then to the code below to make it skip any files that begin with ._ ? Thanks in advance for your help-
Kevin

<?php
/*
-------------------------------------------------------------
|To Display random image use- |
|<?php include "image.php"; ?> |
-------------------------------------------------------------
*/
$dir=opendir("/Apache/htdocs/random/images/");
//This is the directory route to the folder
$directory="www.mysite.com/random/images/";
//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 "<br><img src=\"$directory$image[$randNum]\" $size[3]>";
?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

is it possible for you to use the php tag for php code? i'm not going to try to figurew that out since it's much smaller writng and not in the php tag.. but what i will do is give you something that will get you all the images of a dir because i have that lying around
. infact the mod is so simple that i'll do that too.

Code: Select all

function getimageList(){
  $imagelist=array(); # empty array
  $dir=getcwd().'smilies/'; # get the full path (change this to the dir you want the images from)
  $d=opendir($dir) or die($php_errormsg.'\n'.$dir); # open the directory
  while(false !==($f=readdir($d))){ # while there's more there
    $posimg=$dir . $f; # get the full name
    if(is_file($posimg)){ # there's an image (not . or ..)
      if(!(preg_match('|^._|', $posimg))){
        $nl=strlen($f); $nl=$nl-4;
        $imglist[]=substr($f, 0, $nl); # get just the name
      }
    }
  }
  return $imglist;
}
Last edited by m3rajk on Wed Aug 06, 2003 3:01 pm, edited 1 time in total.
KiltedMile
Forum Newbie
Posts: 2
Joined: Wed Aug 06, 2003 12:44 pm

Question?

Post by KiltedMile »

Then are you suggesting that I insert this into my code? I see how your function works, but how do I implement this into my existing code?

Thanks-
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: Random Images Script Help

Post by m3rajk »

KiltedMile wrote:I have been tinkering with a rotating images script, and have one problem I cannot solve. In our images folder we have several images that are carbon copies, but they are named with a “._” at the beginning and we don’t want to display them with my script. For example, we want to display dumptruck.jpg but we don’t want to display ._dumptruck.jpg. Is there a way I can add an if/then to the code below to make it skip any files that begin with ._ ? Thanks in advance for your help-
Kevin

Code: Select all

<?php
/*
-------------------------------------------------------------
|To Display random image use-    							|
|<?php include "image.php"; ?>                              |
-------------------------------------------------------------
*/
$dir=opendir("/Apache/htdocs/random/images/");
//This is the directory route to the folder
$directory="www.mysite.com/random/images/";
//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 "<br><img src="$directory$image[$randNum]" $size[3]>";
?>
1: see this tag? {size=9}
NEVER ENCAPSULATE CODE IN IT. IT WILL ONLY ANNOY PEOPLE BECAUSE IT MAKES IT SMALLER THAN THE REST OF YOUR POST AND ANNOYING TO FOLLOW BECAUSE OF THE LACK OF SYNTAX HIGHLIGHTING
moving on.....

"your" code uses ereg to check. ereg is posix. posix is always greedy. this is bad. move off ereg.

"your" code looks like it was ripped form a site now that i see it at full size in this box.

"your code" requires it be an included file. mine's a function to pop into your php file. all mine does is get a list, in an array format. instead of using "your" code, i suggest your forget about "your" code, and add the function i gave you to the include file for all pages and modify it to randomly choose an image from the array to return, or to include it in that page.

and here's a hint at modifying it correctly:

instead of the line return $imglist;
replace it with a few lines, which do the following....

declare an empty string
fill it with a randomly chosen item from the array
return the new string


and since i get the errie feeling you'll need it, here's how to get a random element from the array: find: $array[rand(0, count($array)];
Post Reply