Jump to content

[SOLVED] Random image display script help


mattturnbull

Recommended Posts

Hey, I'm currently using the following script to display a random set of images from a folder on my website:-

 

<?
$imglist='';
  //$img_folder is the variable that holds the path to the banner images. Mine is images/tutorials/
// see that you don't forget about the "/" at the end 
$img_folder = "gallery/thumbs/";

  mt_srand((double)microtime()*1000);

  //use the directory class
$imgs = dir($img_folder);

  //read all files from the  directory, checks if are images and ads them to a list (see below how to display flash banners)
while ($file = $imgs->read()) {
   if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
     $imglist .= "$file ";

} closedir($imgs->handle);

  //put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;

//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];

//display image
echo '<img src="'.$img_folder.$image.'" border=0>';

?>

 

Basically the problem I'm having is that some of the images in the directory are larger in physical size than others (see a list below). Heres a list of the current files:-

 

Filename                    Date          Time      Size

10-idea6.jpg            22-Oct-2007 11:46    9k 

11-hood_forest2.jpg    26-Oct-2007 10:33    7k 

12-applebox1.jpg        11-Nov-2007 11:48    7k 

13-applebox2.jpg        11-Nov-2007 11:49    6k 

lrg-14-finalflame.jpg  12-Nov-2007 06:07    35k 

lrg-15-working.jpg      24-Nov-2007 14:56    35k 

lrg-16-finala.jpg      29-Nov-2007 08:47    58k

 

RED = Too large, need to be filtered out

 

I was just wondering if I could modify the above script to filter these larger files out (and thus not displaying them ever) by either denying the name "lrg-....." or using a size limit.

 

Also as another point would it be possible to have the script rescale the images to a certain resolution before displaying them?

 

Im very new to php, any help is welcome :)

Link to comment
Share on other sites

Well welcome to the world of PHP!  Its great!

 

You will find lots of answers to those questions by starting here

http://us2.php.net/manual/en/ref.image.php(just went to php.net and typed in image... php.net is your friend)

 

Now, all of those questions you asked are possible.

 

You can filter by name, by file size... and with the correct libraries, even by image size (I believe... haven't done too much image manipulation with php).  And I'm pretty sure the functions to resize a picture exist as well.  (Actually I'm positive... but again, never used them before.)

 

Now... that said... Probably the easiest way to code would be to filter by file size... but if you had a high resolution small picture... you might end up filtering it too.  And having to name your files with 'lrg' can be annoying.  So which one you choose is up to you.

 

Off the top of my head I can show you what it would look like for the name one though

while ($file = $imgs->read()){
  if ( (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)) && !eregi('^lrg', $file))
  $imglist .= "$file ";
}

 

*woops forgot a ! (not)  fixed*

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.