Skip to content

Instantly share code, notes, and snippets.

@CoolOppo
Last active August 29, 2015 14:01
Show Gist options
  • Save CoolOppo/c9b967f2a955ae07192e to your computer and use it in GitHub Desktop.
Save CoolOppo/c9b967f2a955ae07192e to your computer and use it in GitHub Desktop.
Displays a random GIF image at an unchanging direct URL/URI using PHP
<?php
function random_pic($dir = '/var/www/images') {
$files = glob($dir . '/*.gif');
if (!$files) return false;
$file = array_rand($files);
return $files[$file];
}
function outputImage( $filename ) {
header("Content-Type: image/gif");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($filename ));
ob_clean();
flush();
readfile($filename );
exit;
}
// Get a filename
$filename = random_pic();
// Check that a file was found
if (!$filename) {
header('HTTP/1.1 404 Not Found');
die();
}
// Output the image
outputImage( $filename );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment