<?php

######################################################################
#
# Author: Jason Packer - jhpacker/at/lazyhacker.com
# Version: 1.0 (5/18/01)
#
# random_img.php -- pull a random image out of a directory
#
# call it like this: random_img.php?dir=images/randomimages
# if random image is the dir you want to pull from
# dirs are relative to document root
#
# looks for .gif, .jpg, .png files only & doesn't check to see if
# the files actually are what they claim

# size pulls by height & width ranges and is optional. 
# using size slows things down so don't use that with big directories
#
# example: <img src="random_img.php?dir=images/banners&size=200-300x200"> 
# to pull a random image from DOCUMENT_ROOT/images/banners
# of size 200 to 300 wide and 200 high
#
# There are no restrictions on the use of this code other
# than to include my name in any derived work.  There are
# no warranty for this obviously, but you are welcomed
# to modify, correct, or adapt the code to your needs.  The
# author appreciates if you are willing to submit corrections
# or suggestions for improvement.


# http://www.lazyhacker.com
#
######################################################################


if (!empty($dir)){
  
$docroot getenv("DOCUMENT_ROOT");
  
$filetype filetype("$docroot/$dir");
  if (
$filetype != "dir" && $filetype != "link"){
    echo 
"Error: $dir doesn't appear to be a valid directory";
    exit;
  }

  
$odir opendir("$docroot/$dir");
  if (!
$odir){
    echo 
"Error: can't open dir: $dir";
    exit;
  }
  
$i=0;
  while (
$file readdir($odir)){
    if (
preg_match ("/\.(jpg|gif|png|jpeg)$/i",$file$matches)){
      if (
testsize($file)){
    
$filelist[$i] = $file;
    
$imgtypes[$i] = $matches[1];
    
$i++;
      }
    }
  }
  if (
$i==0){
    echo 
"Error: no matching images in dir: $dir";
    exit;
  }
  
srand ((double) microtime() * 1000000);
  
$randval rand(0,$i-1);
  
$image $filelist[$randval];
  
$imgtype $imgtypes[$randval];
  
#fix jpg mime type
  
if ($imgtype == 'jpg'){
    
$imgtype 'jpeg';
  }

  if (!
$file fopen("$dir/$image""r")){
    echo 
"Error: can't open file: $image";
  }else{
  
header("Expires: Fri, 1 Oct 1999 12:00:00 GMT");
  
header("Content-Type: image/$imgtype");
  
fpassthru($file);
  exit;
  }
}else{
  echo 
"Error: usage is random_img.php?dir=randomdir[&size=widthxheight]\n";
}

function 
testsize($img){
  global 
$size,$docroot,$dir;
  if (empty(
$size)){
    return 
1;
  }
  list(
$width$height) = getimagesize("$docroot/$dir/$img");  
  
preg_match("/(\d+)\-?(\d+)?x(\d+)\-?(\d+)?/",$size,$matches);
  
$min_x $matches[1];
  
$max_x $matches[2];
  
$min_y $matches[3];
  
$max_y $matches[4];
  if (
      (
       ((
$width == $min_x) && empty($max_x)) ||
       ((
$width >= $min_x) && ($width <= $max_x))
       )
      &&
      (
       ((
$height == $min_y) && empty($max_y)) ||
       ((
$height >= $min_y) && ($height <= $max_y))
       )
      )
  {
    return 
1;
  }else{
    return 
0;
  }
}

?>