Skip to content

Instantly share code, notes, and snippets.

@nsuan
Created May 20, 2009 20:50
Show Gist options
  • Save nsuan/115086 to your computer and use it in GitHub Desktop.
Save nsuan/115086 to your computer and use it in GitHub Desktop.
<?php
$def_img_url="wolf-grass.jpg";
//default image to redirect to if no images exist in random dir
//read folder
$folder=opendir(getcwd());
while ($file = readdir($folder)){
$ext=strtolower(substr(trim($file),-4));
if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png") {
$imgs[]=$file;
}
}
closedir($folder);
if (is_array($imgs)){
srand((double)microtime()*1000000);
do {
$ofile = $imgs[rand(0, count($imgs)-1)];
} while($ofile == $_SERVER['HTTP_ETAG']);
$fp = fopen($ofile, "r");
$size = getimagesize($ofile);
header('ETag: ' . $ofile);
header('Content-type: ' . $size['mime']);
header("Content-Length: " . filesize($ofile));
fpassthru($fp);
}else{
header ("location: $def_img_url");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment