- Impact
- 133
Someone asked for a script that would show a random image, only took a few minutes to write, so I thought I might as well share it here.
PHP:
<?php
/**
* Danltn | http://danltn.com/
* No warranty is given to code used.
* I'm scared of lawsuits.
*/
$images = array();
$images[] = 'http://www.escapistmagazine.com/global/media/images/articles/folder/120/zeropunctuation_logo.png';
$images[] = 'http://www.google.com/images/experimental.gif';
$images[] = 'http://www.w3.org/2001/tag/2007/05/TagThursDiagram1.JPG';
// Add as many as you like here.
$image = $images[rand(0, count($images) - 1)];
// Get random image.
switch (strtolower(substr($image, -4, 4)))
{
case "jpeg":
case ".jpg":
$ctype = "image/jpg";
break;
case ".gif":
$ctype = "image/gif";
break;
case ".png":
$ctype = "image/png";
break;
}
// Find the content type, based on the last 4 characters, I could add GDLib here, but I haven't, this was a quick script.
$file = @file_get_contents($image);
// Get the file
if ($file)
{
// Only if the file has successfully been 'grabbed'.
$len = strlen($file);
// Get the length to be kind to the browser.
if (!headers_sent())
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
// Set headers.
header("Content-Type: $ctype");
// Set content type as correct image.
header("Content-Length: " . $len);
// Be kind to the browser by saying the length.
echo $file;
// If it all checks out, send the image.
}
}
?>
Last edited: