randomly including an image from a directory

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
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

randomly including an image from a directory

Post by Daisy Cutter »

is it possible to randomly include an image from a directory? right now I have to upload the image, then add an entry to my random image script.

My random image script is as follows:

Code: Select all

<?php
  $randimg[] = '/img/image.gif';
  ...
  $randimg[] = '/img/lastimage.gif';

  srand ((double) microtime() * 1000000);
  $random_number = rand(0,count($randimg)-1);
?>
then

Code: Select all

&lt;img src="&lt;?php echo $randimg&#1111;$random_number]; ?&gt;" alt="random image" /&gt;
This would help cut down on code size and increase convenience.

EDIT:
although not my purpose this would also be very convenient for an image gallery.
Last edited by Daisy Cutter on Tue Aug 31, 2004 3:31 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can use [php_man]glob[/php_man] or [php_man]readdir[/php_man] to get a list of the files in your directory. Then just use [php_man]rand[/php_man] or [php_man]mt_rand[/php_man] to choose the image from the list.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

This is probably close to what you're looking for

viewtopic.php?t=16746
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

I read that thread, pickle and modified the code:

Code: Select all

<?php

// chanage the directory path
$dir = '/home/mcovey/kafene.org/img';

// set valid extensions
$extentions = array('jpg', 'gif', 'png', 'jpeg', 'BMP', 'bmp', 'PNG', 'JPG', 'JPEG', 'GIF');

$images = array();

$dh = opendir($dir);

   while(($file = readdir($dh)) !== false) {

      // check if it's a file, and it has a valid extension
      if (is_file($dir . $file) && in_array(substr($file, -3), $extentions)) {

         // add image to array
         $images[] = $file;

      }

   }

closedir($dh);

// get 1 random key
$selected = array_rand($images, 1);

// set headers
echo ($images[$selected]);

?>
I'm getting the error:

Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in /home/.halleck/mcovey/kafene.org/randimg.php on line 28
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$dir = '/home/mcovey/kafene.org/img'; needs a trailing slash:
$dir = '/home/mcovey/kafene.org/img/'; for if (is_file($dir . $file) to work correctly.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

FYI: do not use substr to blindly "find" the extension. [php_man]pathinfo[/php_man] and/or regular expressions are much better at handling this.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

thanks.

feyd, I'll look into that. I just copied, pasted and modified from pickle's link.

Right now I'm working on getting the images to link to $filename
Post Reply